文件上传 $image->move() 报错。

andyzu

问题描述

我需要做一个上传的接口,使用了 $request->file(); 下面是代码:

程序代码

foreach ($upload_images as $key => $upload_image) {
                foreach ($upload_image as $image) {
                    if ($image->getUploadName() == '') {
                        return json([
                            'code' => '404701',
                            'msg' => '上传图片未找到',
                            'data' => [],
                        ]);
                    }
                    if ($image->isValid() == false) {
                        return json([
                            'code' => '404704',
                            'msg' => '图片文件无效',
                            'data' => [],
                        ]);
                    }
                    if (!in_array($image->getUploadMimeType(), $allowedTypes)) {
                        return json([
                            // 'IMAGE_INCORRECT_FORMAT' => [404702, '图片格式不正确'],
                            'code' => '404702',
                            'msg' => '图片格式不正确',
                            'data' => [],
                        ]);
                    }

                    if ($image->getSize() > 3145728 ) {
                        return json([
                            'code' => '404703',
                            'msg' => '图片尺寸超出限制',
                            'data' => [],
                        ]);
                    }
                    $filename = uniqid('image_', false) . '.' . $image->getUploadExtension();
                    $image->move(config('app.test_host_name').'images/report_images/'.$filename);
                }
            }

报错信息

{
    "code": 0,
    "msg": "Server Unknown Error",
    "data": {
        "domain": "//localhost:8989/api/v1/upload/images?",
        "method": "POST",
        "request_url": "POST //localhost:8989/api/v1/upload/images?",
        "timestamp": "2023-11-19 16:43:06",
        "client_ip": "127.0.0.1",
        "request_param": [],
        "error_message": "Server Unknown Error",
        "error_trace": [
            "#0 [internal function]: support\\App::{closure}(8192, 'strip_tags(): P...', '/Users/aabbcc/D...', 45)",
            "#1 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/File.php(45): strip_tags(NULL)",
            "#2 /Users/aabbcc/Desktop/webserver/kaiapi/app/controller/api/v1/controller/PublicController.php(62): Webman\\File->move('http://localhos...')",
            "#3 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/App.php(388): app\\controller\\api\\v1\\controller\\PublicController->uploadImages(Object(support\\Request))",
            "#4 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/App.php(349): Webman\\App::Webman\\{closure}(Object(support\\Request))",
            "#5 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/webman/log/src/Middleware.php(58): Webman\\App::Webman\\{closure}(Object(support\\Request))",
            "#6 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/App.php(341): Webman\\Log\\Middleware->process(Object(support\\Request), Object(Closure))",
            "#7 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/webman/cors/src/CORS.php(12): Webman\\App::Webman\\{closure}(Object(support\\Request))",
            "#8 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/App.php(341): Webman\\Cors\\CORS->process(Object(support\\Request), Object(Closure))",
            "#9 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/App.php(560): Webman\\App::Webman\\{closure}(Object(support\\Request))",
            "#10 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/App.php(148): Webman\\App::findRoute(Object(Workerman\\Connection\\TcpConnection), '/api/v1/upload/...', 'POST/api/v1/upl...', Object(support\\Request))",
            "#11 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Connection/TcpConnection.php(646): Webman\\App->onMessage(Object(Workerman\\Connection\\TcpConnection), Object(support\\Request))",
            "#12 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Events/Select.php(311): Workerman\\Connection\\TcpConnection->baseRead(Resource id #311)",
            "#13 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Worker.php(1635): Workerman\\Events\\Select->loop()",
            "#14 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Worker.php(1426): Workerman\\Worker::forkOneWorkerForLinux(Object(Workerman\\Worker))",
            "#15 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Worker.php(1400): Workerman\\Worker::forkWorkersForLinux()",
            "#16 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Worker.php(1785): Workerman\\Worker::forkWorkers()",
            "#17 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Worker.php(1721): Workerman\\Worker::monitorWorkersForLinux()",
            "#18 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/workerman/Worker.php(562): Workerman\\Worker::monitorWorkers()",
            "#19 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\\Worker::runAll()",
            "#20 /Users/aabbcc/Desktop/webserver/kaiapi/start.php(4): support\\App::run()",
            "#21 {main}"
        ],
        "file": "/Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/File.php",
        "line": 45

我打印了 var_dump(config('app.test_host_name').'images/report_images/'.$filename);的内容,如下:
截图
应该都没问题,但是只要一用到 $image->move();方法,就报错。
求大神们帮助!

501 1 0
1个回答

meows

调用站这里为空,你排查看看吧
"#1 /Users/aabbcc/Desktop/webserver/kaiapi/vendor/workerman/webman-framework/src/File.php(45): strip_tags(NULL)"

为什么这里是域名?
ontroller/PublicController.php(62): Webman\File->move('http://localhos...')",

  • andyzu 2023-11-19

    因为我需要把图片挪到另一个域名下的目录里,而不是在 webman 自己的public 目录里。

  • andyzu 2023-11-19

    貌似应该是域名的问题,如果写到 webman 里的 public/images/.... 就可以。

  • walkor 2023-11-19

    $file->move($path) $path必须时本地磁盘路径,不能是网址

  • andyzu 2023-11-19

    老大,那想我这样的需求,必须把上传的图片挪到另一个网址下,如何实现?求教

  • walkor 2023-11-19

    不是同一个服务器?

  • andyzu 2023-11-19

    不同的服务器。同一个服务器,我用 copy() 方法,应该能解决。

  • walkor 2023-11-19

    可以curl传过去,和浏览器上传文件原理一样,只不过客户端是curl

  • andyzu 2023-11-19

    好的,感谢老大!

×
🔝