我正在尝试将文件从FTP远程服务器复制到另一台FTP远程服务器,我收到警告:
Warning: copy(): The first argument to copy() function cannot be a directory in
我已经仔细检查过我的文件夹名称和文件路径,一切正确。我可以在嵌套copy()
之外成功使用foreach()
,但只要我添加嵌套循环并将copy()
添加到chokes中就可以了。
这是我的代码:
// set-up basic connection
$one_connection = ftp_connect("ftp.***.com");
$two_connection = ftp_connect("ftp.***.com");
// login with username and password
$one_login_result = ftp_login($one_connection, $one_user, $one_pass);
$two_login_result = ftp_login($two_connection, $two_user, $two_pass);
// get a list of directories on ftp server
$directories = ftp_nlist($one_connection, "ftp_images/rsr_number/");
// log start time
$start_time = date('Hi');
// loop through and pull all images from each directory
foreach($directories as $dir)
{
// get list of images in directory
$images = ftp_nlist($one_connection, "ftp_images/img_number/".$dir);
foreach($images as $img)
{
copy('ftp://user:pass@ftp.****.com/ftp_images/img_number/' .$dir . '/' . $img, 'ftp://user:pass@ftp.***.com/public_html/temp/' . $img);
}
}
ftp_close($one_connection);
ftp_close($two_connection);
为什么我收到这个警告?
另外,这是连接两台远程FTP服务器的正确方法吗?
修改
这是var_dump()
的{{1}}:
$directories
答案 0 :(得分:2)
这里试试这个:
使用的课程是here。
<?php
// use object oriented method
// Class found here: http://geneticcoder.blogspot.com/2015/04/class-for-doing-ftp-stuff.html
$ftp1 = new ftp($host_one, $one_user, $one_pass);
$ftp2 = new ftp($host_two, $two_user, $two_pass);
//change to the correct path of the temp directory
$ftp2->change_dir("public_html/temp/");
// get a list of directories on ftp server
$directories = $ftp1->list_all("ftp_images/rsr_number/");
// loop through and pull all images from each directory
foreach($directories as $dir){
// FTP almost always sends these dot-dealies, filter them out
if($dir == "." || $dir == "..") continue;
// change the current directory
$current_dir = "ftp_images/rsr_number/$dir";
$ftp1->change_dir($current_dir);
// get list of images in directory
$images = $ftp1->list_all();
foreach($images as $img){
// FTP almost always sends these dot-dealies, filter them out
if($img == "." || $img == "..") continue;
// copy the file from a to b
// create a temporary file on the server to store the file we're moving
$tmpname = realpath(dirname(__FILE__))."/".microtime();
// temporarily put the file on the current server
$file = $ftp1->get("$current_dir/$img", $tmpname);
// move file from current server to remote server
$ftp2->put($tmpname);
// remove the file from the current server
unlink($tmpname);
}
}
$ftp1->close();
$ftp2->close();