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

Laravel: The file was not uploaded due to an unknown error

我正在尝试将文件上传到两个不同的位置。涂层是 /2x//3x/。它以 3x 的速度上传文件,但不以 2x 的速度上传文件并抛出此错误:

The file was not uploaded due to an unknown error

这是我正在做的事情:

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

有什么帮助吗?

你可以试试这个:

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

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

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

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

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

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

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

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