我需要将同一个文件上传到FTP的两个不同位置。是否有一种方法可以将文件从FTP复制到另一个位置,而不是再次上传它?谢谢。
在FTP协议中,没有标准方法可以复制远程文件。但是,一些FTP服务器支持专有或非标准扩展来实现此功能。
SITE CPFR
/CPTO
FTP扩展(例如ProFTPD mod_copy
模块支持)。
- 如果上述扩展不可用,则自动通过本地临时副本进行复制。
(我是WinSCP的作者)
顺便提一下:人们经常混淆移动和复制。如果你真的想要移动,那就是完全不同的问题了。在FTP上移动文件得到了广泛支持。
function copyFile($filePath, $newFilePath)
{
$ftp1 = ftp_connect('192.168.1.1');
$ftp2 = ftp_connect('192.168.1.1');
ftp_raw($ftp1, "USER ftpUsername");
ftp_raw($ftp1, "PASS mypassword");
ftp_raw($ftp2, "USER ftpUsername");
ftp_raw($ftp2, "PASS mypassword");
$res = ftp_raw($ftp2, "PASV");
$addressAndPort = substr($res[0], strpos($res[0], '(') + 1);
$addressAndPort = substr($addressAndPort, 0, strpos($addressAndPort, ')'));
ftp_raw($ftp1, "CWD ." . dirname($newFilePath));
ftp_raw($ftp2, "CWD ." . dirname($filePath));
ftp_raw($ftp1, "PORT ".$addressAndPort);
ftp_raw($ftp1, "STOR " . basename($newFilePath));
ftp_raw($ftp2, "RETR " . basename($filePath));
ftp_raw($ftp1, "QUIT");
ftp_raw($ftp2, "QUIT");
}
我通过使用WebDrive将FTP挂载为本地文件夹,然后直接使用FileZilla将文件“下载”到该文件夹中来完成了这个操作。虽然速度比正常下载要慢一些,但你不需要在硬盘上占用空间。
以下是使用 PHP cUrl 的另一种解决方法,通过从本地机器提供参数并报告结果来执行服务器上的复制请求:
本地代码: 在这个简单的测试例程中,我想将倾斜的塔照片复制到正确的文件夹 Pisa 中:
$ch = curl_init();
$data = array ('pic' => 'leaningtower', 'folder' => 'Pisa');
curl_setopt($ch, CURLOPT_URL,"http://travelphotos.com/copypic.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
$pic = $_POST["pic"];
$folder = $_POST["folder"];
if (!$pic || !$folder) exit();
$sourcePath = "./unsortedpics/".$pic.".jpg";
$destPath = "./sortedpics/".$folder."/".$pic.".jpg";
if (!file_exists($sourcePath )) exit("Source file not found");
if (!is_dir("./sortedpics/".$folder)) exit("Invalid destination folder");
if (!copy($sourcePath , $destPath)) exit("Copy not successful");
echo "File copied";
您可以通过 C-Panel 完成此操作。
完成!
您可以将要复制的文件重命名为所需结果的完整路径。
例如: 如果您想将文件“file.txt”移动到文件夹“NewFolder”中,可以这样写:
ftp> rename file.txt NewFolder/file.txt
这对我有用。