【问题标题】:Laravel 5.2 'The file "Cover.jpg" was not uploaded due to an unknown error.'Laravel 5.2 '由于未知错误,文件“Cover.jpg”未上传。'
【发布时间】:2016-08-08 10:12:47
【问题描述】:

我使用 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";
            }

总是返回 "success",所以函数返回 'true' 但 Laravel 会抛出错误。但是循环中的相同函数“移动”不会返回错误。 所有图片都将成功上传和移动。

【问题讨论】:

  • 和我一样的问题,你找到解决办法了吗?
  • 你没有给出完整的路径,这就是它不起作用的原因。

标签: php laravel image-uploading


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2016-08-15
    相关资源
    最近更新 更多