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

如何用PHP将本地文件夹所有文件上传至SFTP?代码问题咨询

如何通过PHP批量上传本地文件/目录到SFTP

当然可以实现本地目录上传到SFTP远程目录,或者批量上传目录下的所有文件!你目前的代码确实只能处理单个文件,因为file_get_contents并不支持通配符(像*.*这种写法它完全识别不了),而且远程路径的写法也有问题,咱们一步步来解决。

先说说你现有代码的问题

  • $file = "C:/incoming/360/upload/".date("Ymd")."/*.*";:file_get_contents不认识通配符*.*,它会把整个字符串当成一个完整的文件名,自然找不到对应的文件
  • $dest = "/testing/*.*";:远程SFTP服务器也不会解析通配符,必须指定具体的文件名,不能用*.*模糊匹配
  • 路径拼接错误:ssh2.sftp://{$sftp}/testing/{$dest}会变成ssh2.sftp://{$sftp}/testing//testing/*.*,这明显是无效的远程路径

方案1:批量上传目录下的所有文件(不含子目录)

用glob函数可以轻松获取本地目录下的所有文件,然后逐个上传到远程SFTP目录:

// 本地目标目录(记得末尾加斜杠)
$localDir = "C:/incoming/360/upload/" . date("Ymd") . "/";
// SFTP远程目录(同样末尾加斜杠)
$remoteDir = "/testing/";

// 获取本地目录下的所有文件(*.*匹配所有带后缀的文件,若要所有文件可以用*)
$localFiles = glob($localDir . "*.*");

foreach ($localFiles as $localFile) {
    // 提取文件名(比如从完整路径里拿到"example.txt")
    $fileName = basename($localFile);
    // 拼接远程文件的完整SFTP路径
    $remoteFilePath = "ssh2.sftp://{$sftp}" . $remoteDir . $fileName;

    // 读取本地文件并上传到远程
    $fileContent = file_get_contents($localFile);
    if ($fileContent !== false) {
        file_put_contents($remoteFilePath, $fileContent);
        echo "文件 {$fileName} 上传成功!\n";
    } else {
        echo "读取本地文件 {$localFile} 失败\n";
    }
}

方案2:上传整个目录(包含所有子目录和文件)

如果需要连子目录一起上传,就得用递归的方式处理,这里用DirectoryIterator来遍历目录:

/**
 * 递归上传本地目录到SFTP远程目录
 * @param resource $sftp 已连接的SFTP资源
 * @param string $localDir 本地目录路径
 * @param string $remoteDir 远程SFTP目录路径
 */
function uploadSFTPDirectory($sftp, $localDir, $remoteDir) {
    // 确保远程目录存在,不存在则递归创建
    $remoteFullPath = "ssh2.sftp://{$sftp}" . $remoteDir;
    if (!file_exists($remoteFullPath)) {
        mkdir($remoteFullPath, 0755, true);
    }

    // 遍历本地目录
    $dirIterator = new DirectoryIterator($localDir);
    foreach ($dirIterator as $fileInfo) {
        // 跳过.和..这两个特殊目录
        if ($fileInfo->isDot()) continue;

        $localFilePath = $fileInfo->getPathname();
        $remoteFilePath = $remoteDir . $fileInfo->getFilename();

        if ($fileInfo->isDir()) {
            // 如果是子目录,递归调用上传
            uploadSFTPDirectory($sftp, $localFilePath, $remoteFilePath . "/");
        } else {
            // 如果是文件,执行上传
            $remoteFullFilePath = "ssh2.sftp://{$sftp}" . $remoteFilePath;
            $fileContent = file_get_contents($localFilePath);
            if ($fileContent !== false) {
                file_put_contents($remoteFullFilePath, $fileContent);
                echo "上传成功:{$remoteFilePath}\n";
            } else {
                echo "读取失败:{$localFilePath}\n";
            }
        }
    }
}

// 使用示例
$localRootDir = "C:/incoming/360/upload/" . date("Ymd") . "/";
$remoteRootDir = "/testing/";
uploadSFTPDirectory($sftp, $localRootDir, $remoteRootDir);

另外要注意,你得确保已经成功建立了SFTP连接(也就是代码里的$sftp资源是有效的),如果还没建立连接,可以用下面的代码完成:

// 示例:建立SFTP连接
$host = "你的SFTP服务器地址";
$port = 22;
$username = "你的用户名";
$password = "你的密码";

$connection = ssh2_connect($host, $port);
if (ssh2_auth_password($connection, $username, $password)) {
    $sftp = ssh2_sftp($connection);
    // 然后调用上面的上传函数
} else {
    die("SFTP认证失败");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:03:42