Laravel 5.2'文件"Cover.jpg"由于未知错误未上传." [英] Laravel 5.2 'The file "Cover.jpg" was not uploaded due to an unknown error.'

查看:122
本文介绍了Laravel 5.2'文件"Cover.jpg"由于未知错误未上传."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的代码:

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抛出错误.但是循环中的相同功能移动"不会返回错误. 所有图像都将成功上传和移动.

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.

推荐答案

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

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

这篇关于Laravel 5.2'文件"Cover.jpg"由于未知错误未上传."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆