laravel无法将图像数据写入路径 [英] laravel Can't write image data to path
问题描述
不能存储图像总是说不能写图像数据 错误:无法将图像数据写入路径(E:\ work \ iShop-Admin-backend \ storage/app/public/images/p/4069/11016/4069_11016_600x600.jpg) 我的代码:
can't store image it's always say can't write image data error: Can't write image data to path (E:\work\iShop-Admin-backend\storage/app/public/images/p/4069/11016/4069_11016_600x600.jpg) my code :
$destinationPath = storage_path() . '/app/public/images/p/' . $product->id_product . '/';
if (!File::exists($destinationPath)) {
File::makeDirectory($destinationPath);
}
for ($i = 1; $i<=6; $i++){
if($row["image_{$i}"] != "")
$value = $row["image_{$i}"];
$contents = file_get_contents($value);
$imageBase64 = base64_encode($contents);
$filetype = \File::extension($value);
//$ext = ltrim($filetype, 'image/');
$ext = 'image/' . $filetype;
$media = 'data:' . $ext . ';base64,' . $imageBase64;
//Log::info($media);
//Log::info($ext);
if (!empty($media)) {
$position = ProductImage::where('id_product', $product->id_product)->max('position');
$data = array(
'id_product' => $product->id_product,
'position' => $position ? $position + 1 : 1,
'extension' => '.' . $filetype
);
if ($i == 1) {
$data['cover'] = 1;
} else {
$data['cover'] = 0;
}
$image = ProductImage::create($data);
LogController::log('STORE', 'catalog/product/upload/' . $product->id_product, $product->id_product, 'PRODUCT', 'add image to product where id_product=' . $product->id_product, null, $image);
if (!File::exists($destinationPath . $image->id_image)) {
File::makeDirectory($destinationPath . $image->id_image);
$destinationPath = $destinationPath . $image->id_image . '/';
$height = Image::make($media)->height();
$width = Image::make($media)->width();
Image::make($media)->resize(600, 600)->save($destinationPath . $product->id_product . '_' . $image->id_image . '_600x600' . '.' . $filetype);
Image::make($media)->resize(270, 270)->save($destinationPath . $product->id_product . '_' . $image->id_image . '_270x270' . '.' . $filetype);
Image::make($media)->resize(140, 140)->save($destinationPath . $product->id_product . '_' . $image->id_image . '_140x140' . '.' . $filetype);
Image::make($media)->resize(80, 80)->save($destinationPath . $product->id_product . '_' . $image->id_image . '_80x80' . '.' . $filetype);
$destinationPath = storage_path() . '/app/public/images/p/' . $product->id_product . '/';
}
}
}
如何解决?
推荐答案
与此相关,您需要确保文件夹路径存在并且设置了正确的权限.
Along with this, you need to make sure the folder path exists and which has right permissions set.
$relPath = 'img/product/';
if (!file_exists(public_path($relPath))) {
mkdir(public_path($relPath), 777, true);
}
$relPath
是相对于public
目录的路径.
Where $relPath
is the path relative to public
directory.
但是,此要求特定于Windows.在Linux中,如果文件夹目录不存在,则会创建该文件夹目录.
This requirement is however windows specific. In linux, folder directory will be created if it does not exist.
这篇关于laravel无法将图像数据写入路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!