Laravel:由于未知错误,文件未上传

时间:2017-07-07 07:24:30

标签: php file upload laravel-5.3

我正在尝试将文件上传到两个不同的位置。 lcoations为/2x/ adn /3x/。它在3x上传文件但在2x上没有上传并抛出此错误:

  

由于未知错误

,文件未上传

这就是我在做的事情:

$photo = $request->file('photo');

    if (isset($photo)) {
        if ($photo != null || $photo != '') {

            $imageSize = getimagesize($photo);
            $resolution = $imageSize[0] . 'x' . $imageSize[1];

            if ($resolution == '300x300' || $resolution == '450x450') {

                if (!file_exists(base_path('uploads/custom_avatar'))) {
                    mkdir(base_path('uploads/custom_avatar'), 0777, true);
                }

                $resolution = "3x";

                $uploadPath = base_path('uploads/custom_avatar/' . $resolution . '/');

                $otherImageResolution = '2x';
                $otherImagePath = base_path('uploads/custom_avatar/' . $otherImageResolution . '/');
                //echo $otherImagePath;exit;
                // saving image
                $fileName = $child->id . '_' . time() . '.png';

                $photo->move($uploadPath, $fileName);
                $photo->move($otherImagePath, $fileName);

                // creating records
                $childImage = Images::addPhoto($child->id, $fileName, $resolution);
                $otherImage = Images::addPhoto($child->id, $fileName, $otherImageResolution);

                if ($childImage && $otherImage) {
                    $result = Child::createChildResponseData($child);
                    \Log::info('Child avatar added Successfully' . json_encode($childImage));
                    return response()->json([
                        'status' => $this->SUCCESS,
                        'response' => $result,
                    ], $this->SUCCESS);
                } 

任何帮助?

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

$request->file('photo')->move($destination_path, $file_name);

如果需要,在路径和文件名之间添加DIRECTORY_SEPARATOR 在新位置复制该文件

copy($destination_path.$file_name, $new_path.$new_file_name);

答案 1 :(得分:0)

检查您的代码是否两次运行了文件上传代码。

我遇到了同样的问题,然后发现我的文件上传代码运行了两次。

评论其中之一后,效果很好。