Laravel:由于未知错误,文件未上传 [英] Laravel: The file was not uploaded due to an unknown error

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

问题描述

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

I am trying to upload a file to two different locations. The lcoations being /2x/ adn /3x/. It uploads the file on 3x but doesn't on 2x and throws this error:

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

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,并且 将该文件复制到新位置

Add DIRECTORY_SEPARATOR between path and filename if needed and copy that file at new location

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

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

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