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

问题描述 投票:0回答:4

我正在尝试将文件上传到两个不同的位置。涂层为

/2x/
/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);
                } 

有什么帮助吗?

php file upload laravel-5.3
4个回答
22
投票

如果您的文件上传代码运行了两次,请检查您的代码。

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

评论其中一个后,它工作正常。


5
投票

你可以试试这个:

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

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

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

1
投票

如果您的文件上传代码运行了两次,请检查您的代码。 你可以检查这部分代码。确保输入正确,不要重复两次。

   // Original size upload file
    $section_image_file->move($folder, $section_image_name);

0
投票

检查是否调用了任何方法(例如 $file->getSize()),因为它也调用了 move 方法。

© www.soinside.com 2019 - 2024. All rights reserved.