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

如何修改PHP代码将文件保存到指定文件夹

修改文件保存目录至up文件夹的解决方案

问题根源

你代码里已经定义了$target_Path = "up/",但调用copy()函数时根本没用到这个变量,直接用了只包含文件名的$save_to,所以文件还是会存到根目录。另外原代码存在语法错误,这也导致你手动添加的代码没生效。

修复步骤

  1. 补全语法错误
    原代码中$save_to=basename($file_url)这一行末尾缺少分号,先补上:

    $save_to=basename($file_url);
    
  2. 拼接正确的保存路径
    将$target_Path和$save_to拼接成完整的目标路径,替换copy()里的目标参数。修改后的PHP核心代码如下:

    <?php
    if ($_SERVER['REQUEST_METHOD']=="POST") {
        set_time_limit(0);
        $file_url=$_POST['url_file_url'];
        $submitted_password=$_POST["psw_config_pass"];
        $original_password=$config_password;
        $save_to=basename($file_url); // 补充分号
        $target_Path = "up/";
        if ($original_password==$submitted_password) {
            $full_save_path = $target_Path . $save_to; // 拼接up目录与文件名
            if (!copy($file_url, $full_save_path)) {
                echo "<div class='alert alert-danger'><strong><i class='glyphicon glyphicon-remove'></i>Upload failed!</strong> The file $save_to didn't uploaded on the server.</div>";
            } else {
                echo "<div class='alert alert-success'><strong><i class='glyphicon glyphicon-ok'></i>Upload Successful!</strong> The file $save_to has been successfully uploaded to the up/ folder.</div>";
            }
        }
    }
    ?>
    
  3. 检查文件夹配置

    • 确认服务器上已经创建了up文件夹
    • 给该文件夹设置可写入权限(推荐755,部分服务器需设置为777)

内容的提问来源于stack exchange,提问作者Asad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 21:34:56