简体   繁体   中英

laravel Can't write image data to path

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 . '/';
                }

            }

        }

how to solve it ?

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);
    }

Where $relPath is the path relative to public directory.

This requirement is however windows specific. In linux, folder directory will be created if it does not exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM