Laravel 5.2'文件" Cover.jpg"因未知错误而未上传。'

时间:2016-08-08 10:12:48

标签: php laravel image-uploading

我使用Laravel 5.2并收到此错误。

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

我的代码:

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

声明

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

始终返回"成功",因此该函数返回' true'但是Laravel引发了一个错误。但同样的功能“移动”#39;在循环中不会返回错误。 所有图片都会成功上传和移动。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,因为我没有提供目标文件夹的完整路径

{  


"0" : {
      "ID": "Doc1", 
      "URL":"http://192.168.13.158/31818_E-(1).PDF"
   },

   "1" : {
      "ID": "Doc1", 
      "URL":"http://192.168.13.158/31818_P-(1).pdf"
   },

   "2" : {
      "ID": "Doc1", 
      "URL":"http://192.168.13.158/31818_P-(2)%20(1).pdf"
   },

   "3" : {
      "ID": "Doc1", 
      "URL":"http://192.168.13.158/31818_T-(1).pdf"
   },