是否可以使用FTP
中的PHP
方法将文件移动到特定文件夹或声明特定文件夹以下载文件?
简而言之,我正在下载10,000多个文件,我希望它们进入我已创建的某个文件夹。我使用FTP
连接从我的脚本下载文件,我循环遍历FTP
服务器中的每个文件。他们都下载了(这需要很长时间) - 我只需要声明一个特定的路径或将文件移动到该文件夹。
以下是代码:
function ftp_sync($dir, $conn_id){
if($dir !== '.'){
if(ftp_chdir($conn_id, $dir) === FALSE){
echo 'Change directory failed: ' . $dir . PHP_EOL;
return;
}
chdir($dir);
}
$contents = ftp_nlist($conn_id, '.');
foreach($contents as $file){
if($file == '.' || $file == '..'){
continue;
}
if(@ftp_chdir($conn_id, $file)){
ftp_chdir($conn_id, "..");
ftp_sync($file, $conn_id);
} else {
ftp_get($conn_id, $file, $file, FTP_BINARY);
//TODO: Download the files into a specific directory
}
}
ftp_chdir($conn_id, '..');
chdir('..');
}
$ftp_server = 'server';
$user = 'user';
$password = 'password';
$document_root = '/';
$sync_path = 'Web_Images';
$conn_id = ftp_connect($ftp_server);
if ($conn_id) {
$login_result = ftp_login($conn_id, $user, $password);
ftp_pasv($conn_id, true);
if ($login_result) {
ftp_chdir($conn_id, $document_root);
ftp_sync($sync_path, $conn_id);
ftp_close($conn_id);
} else {
echo 'login to server failed!' . PHP_EOL;
}
} else {
echo 'connection to server failed!';
}
echo 'done.' . PHP_EOL;
答案 0 :(得分:2)
ftp_get($conn_id, $file, $file, FTP_BINARY);
应该能够将您的远程文件放在任何您想要的位置作为默认值,您只需在本地参数中指明该位置:
# Where ever you want to download local files to
$dir = __DIR__.'/my/specific/path/';
# See if directory exists, create if not
if(!is_dir($dir))
mkdir($dir,0755,true);
# Saves the file(s) into the $dir folder with the same name as the remote file
ftp_get($conn_id, $dir.$file, $file, FTP_BINARY);