You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

FTP服务器文件上传失败求助(附PHP/HTML代码)

排查FTP上传失败问题的解决方案

我来帮你一步步排查这个FTP上传失败的问题,结合你的代码和WordPress环境,先从几个最常见的关键点入手:

1. 核心问题:目标路径需要完整文件名(而非目录)

你当前的$destination_file只写了目录路径,但ftp_put()函数需要的是完整的目标文件名(包含文件名和后缀),不是文件夹路径。这是最容易踩的坑!

比如你上传的文件是photo.jpg,目标路径应该是/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums/photo.jpg,而不是只到Albums目录。

你可以通过$_FILES['file']['name']获取上传文件的原名,拼接成完整路径:

$filename = $_FILES['file']['name'];
$destination_file = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums/" . $filename;

2. 调整被动模式的调用时机

你现在在ftp_connect()后直接开启被动模式,但部分FTP服务器要求登录成功后才能切换被动模式,否则会导致上传失败。调整代码顺序:

// 先登录
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// 登录成功后再开启被动模式
ftp_pasv($conn_id, true);

3. 开启错误调试,获取详细失败原因

默认情况下ftp_put()失败只会返回布尔值,没有具体错误信息。你可以开启PHP错误报告,或者在上传失败后打印最后错误:

// 在代码开头开启错误显示
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 上传失败时打印详细错误
if (!$upload) {
    echo "FTP upload has failed!<br>";
    $last_error = error_get_last();
    if ($last_error) {
        echo "错误详情:" . $last_error['message'];
    }
}

4. 检查目标目录权限与可用性

确认目标目录/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums有写入权限(通常设置为755或775),并且FTP用户能访问该目录。可以在上传前添加目录检查:

$destination_dir = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
// 尝试切换到目标目录,验证是否可访问
if (!ftp_chdir($conn_id, $destination_dir)) {
    echo "无法访问目标目录,请检查路径或权限<br>";
    // 尝试自动创建目录(如果服务器允许)
    if (ftp_mkdir($conn_id, $destination_dir)) {
        echo "已自动创建目标目录<br>";
        ftp_chdir($conn_id, $destination_dir);
    } else {
        ftp_close($conn_id);
        exit;
    }
}

5. 检查临时文件的可用性

确保PHP能读取上传的临时文件,添加检查:

$source_file = $_FILES['file']['tmp_name'];
if (!file_exists($source_file) || !is_readable($source_file)) {
    echo "临时文件不可用,请检查PHP上传配置<br>";
    exit;
}

修改后的完整代码

结合以上调整,你的PHP代码可以改成这样:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 先检查文件上传是否正常
if (empty($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    echo "文件上传初始化失败,请检查文件大小或格式";
    return;
}

$ftp_server = "ftp.myserver.nl";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_dir = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
$source_file = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$destination_file = $destination_dir . "/" . $filename;

// 检查临时文件
if (!file_exists($source_file) || !is_readable($source_file)) {
    echo "无法读取临时文件,请检查PHP上传权限";
    exit;
}

// 建立FTP连接
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// 验证连接
if ((!$conn_id) || (!$login_result)) {
    echo "FTP连接失败!尝试连接服务器:$ftp_server,用户:$ftp_user_name";
    exit;
} else {
    echo "已连接到$ftp_server,用户:$ftp_user_name<br>";
}

// 登录后开启被动模式
ftp_pasv($conn_id, true);

// 检查并创建目标目录
if (!ftp_chdir($conn_id, $destination_dir)) {
    echo "目标目录不可访问,尝试创建目录...<br>";
    if (ftp_mkdir($conn_id, $destination_dir)) {
        echo "目录创建成功<br>";
        ftp_chdir($conn_id, $destination_dir);
    } else {
        echo "目录创建失败,请检查FTP用户权限";
        ftp_close($conn_id);
        exit;
    }
}

// 执行上传
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// 检查上传结果
if (!$upload) {
    echo "FTP上传失败!<br>";
    $last_error = error_get_last();
    if ($last_error) {
        echo "错误详情:" . $last_error['message'];
    }
} else {
    echo "文件上传成功!已将$source_file上传至$ftp_server,路径:$destination_file";
}

// 关闭FTP连接
ftp_close($conn_id);
?>

额外注意事项(WordPress环境)

  • 检查WordPress后台的媒体上传限制,以及PHP配置中的upload_max_filesizepost_max_size,如果文件超过限制也会导致上传失败;
  • 部分FTP服务器使用相对路径而非绝对路径,可以尝试将$destination_dir改成相对路径(比如wp-content/plugins/AbonneerProgrammas/Albums),取决于你的FTP登录根目录。

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

火山引擎 最新活动