laravel无法将图像数据写入路径

时间:2019-12-04 22:49:07

标签: php laravel image

不能存储图像总是说不能写入图像数据 错误:无法将图像数据写入路径(E:\ work \ iShop-Admin-backend \ storage / app / public / images / p / 4069/11016 / 4069_11016_600x600.jpg) 我的代码:

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

            }

        }

如何解决?

1 个答案:

答案 0 :(得分:0)

与此相关,您需要确保文件夹路径存在并且设置了正确的权限。

/**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

$relPath = 'img/product/'; if (!file_exists(public_path($relPath))) { mkdir(public_path($relPath), 777, true); } 是相对于$relPath目录的路径。

但是,此要求特定于Windows。在Linux中,如果文件夹目录不存在,则会创建该文件夹目录。