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

Saa*_*ani 3 php upload file 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);
                } 
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

小智 16

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

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

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


Sur*_*wat 5

你可以试试这个:

$request->file('photo')->move($destination_path, $file_name);
Run Code Online (Sandbox Code Playgroud)

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

copy($destination_path.$file_name, $new_path.$new_file_name);
Run Code Online (Sandbox Code Playgroud)