You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将图片移动至其他子域名的public目录?

解决方案

要将图片移动到目标子域名对应的public路径,核心是构造正确的目标目录路径,替换原路径中的web/19部分为web21,具体修改代码如下:

修改后的代码

<?php

use Illuminate\Support\Facades\File;

if ($request->front_picture_path != '') {
    $imageName3 = time() . '_front.' . $request->front_picture_path->getClientOriginalExtension();

    // 定义目标子域名的public根目录
    $targetPublicRoot = '/test/abc/test/client12/web21/web/public';
    // 拼接最终上传目录
    $targetUploadDir = $targetPublicRoot . '/uploads_ext/uploads';

    // 自动创建目录(如果不存在),确保写入权限
    File::makeDirectory($targetUploadDir, 0755, true, true);

    // 移动文件到目标目录
    $request->front_picture_path->move($targetUploadDir, $imageName3);

    // 存储文件的完整路径
    $imgdata['right_picture_path'] = $targetUploadDir . '/' . $imageName3;
}

关键说明

  • 路径替换:直接将原路径中的web/19/web/public替换为web21/web/public,构造目标public目录的绝对路径。
  • 权限与目录创建:提前用File::makeDirectory创建目录并设置权限(0755),避免因目录不存在或权限不足导致上传失败。
  • 灵活配置(可选):如果目标路径需要动态调整,建议将路径配置到.env文件中,比如:
    TARGET_PUBLIC_PATH=/test/abc/test/client12/web21/web/public
    
    然后在代码中用env('TARGET_PUBLIC_PATH')获取,无需硬编码路径。

内容的提问来源于stack exchange,提问作者Jayasri S.V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 10:24:54