Laravel 5.2 'The file “Cover.jpg” was not uploaded due to an unknown error.'

浪子不回头ぞ 提交于 2020-01-04 05:41:08

问题


I using Laravel 5.2 and got this error.

FileException in UploadedFile.php line 235: The file "Cover.jpg" was not uploaded due to an unknown error. 

   1. in UploadedFile.php line 235 at UploadedFile->move('productImages', '20160808094822_a3f390d88e4c41f2747bfa2f1b5f87db.jpg')
   2. in ProductController.php line 144

My Code:

public static function imageUpload(Request $request, $productId, $type = 'image') {
        /* Set file destination */
        $destination = 'productImages';

        if ($request->hasFile('cover') OR $request->hasFile('images')) {

            /* Single file - cover */
            if ($request->hasFile('cover')) {
                $filename = date('YmdHis') . '_' . md5($productId) . '.jpg';

                $filepath = "/" . $destination . "/" . $filename;

                $prodImage = new Product_Images;
                $prodImage->productId = $productId;
                $prodImage->imagePath = $filepath;
                $prodImage->cover = ($type == 'cover' ? 'yes' : 'no');
                $prodImage->save();

                if ($request->file('cover')->move($destination, $filename)) {
                    echo "success";
                }
                else {
                    echo "error";
                }
            }

            /* Process multiple files */
            if (count($request->file('images')) > 0) {
                foreach ($request->file('images') as $image) {
                    $filename = date('YmdHis') . '_' . md5($image->getClientOriginalName()) . '.jpg';

                    $filepath = "/" . $destination . "/" . $filename;

                    $prodImage = new Product_Images;
                    $prodImage->productId = $productId;
                    $prodImage->imagePath = $filepath;
                    $prodImage->cover = ($type == 'cover' ? 'yes' : 'no');
                    $prodImage->save();

                    $image->move($destination, $filename);
                }
            }
        }



    if ($request->hasFile('images')) {
        self::imageUpload($request, $product->id);
    }
    if ($request->hasFile('cover')) {
        self::imageUpload($request, $product->id, 'cover');
    }

The Statement

  if ($request->file('cover')->move($destination, $filename)) {
                echo "success";
            }
            else {
                echo "error";
            }

always returns "success", so the function returns 'true' but Laravel throws an error. But the same function 'move' in the loop doesn't return an error. All images will successful uploaded and moved.


回答1:


I was having the same issue because I was not giving the complete path to the destination folder

if (Input::file('product_image')->isValid()) {
            $extension = Input::file('product_image')->getClientOriginalExtension(); // getting image extension
            $fileName = $unique_prefix . rand(11111,99999).'.'.$extension; // renameing image
            Input::file('product_image')->move(public_path().$destinationPath, $fileName); // uploading file to given path
            dd(public_path().$destinationPath);
        }


来源:https://stackoverflow.com/questions/38826546/laravel-5-2-the-file-cover-jpg-was-not-uploaded-due-to-an-unknown-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!