gpt4 book ai didi

php - 从带有路径列表的FTP服务器下载文件并下载列出的每个文件

转载 作者:行者123 更新时间:2023-12-02 23:54:13 26 4
gpt4 key购买 nike

目前,我正在一个项目中,需要从ftp服务器下载图像以将其放置在网站上。

图像的路径位于服务器上压缩的txt文件中。每个图像的路径如下所示:

pc33c1_26126_08_hd.jpg /photos/pvo/transfertvo/photos/pc33c1/pc33c1_x48_08_hd.jpg 1a71ffb90de7628b8b1585b7e85c68e7

获取照片的方式应如下所示:

get /photos/pvo/transfertvo/photos/pc33c1/pc33c1_x48_08_hd.jpg pc33c1_26126_08_hd.jpg

此示例由托管图像的公司提供。

当我使用PowerShell连接到ftp时,有两个目录可用-一个位于zip文件所在的目录,另一个目录似乎为空。当您查看.txt文件中的路径时,您可以清楚地看到图片实际上位于似乎为空的第二个目录内。在PowerShell中使用get命令时,可以下载图片,但只能一张一张地下载(并且需要下载数百张图片)。

我尝试编写一个PowerShell脚本,该脚本会遍历.txt文件并“采用”查找图像所需的URL。首先,我需要连接到FTP,但是以下代码不起作用:

$FTPServer = "ftp.example.com"
$FTPUsername = "user"
$FTPPassword = "pass"
$credential = New-Object System.Net.NetworkCredential($FTPUsername, $FTPPassword)

$FTP = [System.Net.FtpWebRequest]::Create($FTPServer)

$FTP.Credentials=$credential
$FTP.UseBinary = 1
$FTP.KeepAlive = 0

从FTP下载后,第二段代码应该通过.txt文件,但首先我无法连接到服务器来完成该操作。
$f = Get-Content "photos.txt"
$i = 0

foreach ($line in $f) {

$fields = $line.Split("`t")

$First = $fields[0]
$Second = $fields[1]
$Third = $fields[2]
$number = $i++

$url = $Second +" "+ $First

write-host "Title is: "$First
write-host "Path is: "$Second
write-host "Hash is: "$Third
write-host "Number is: " $number
Write-Host "This is the url: " $url
get $url
Write-Host ""
}

这应该做什么,它应该遍历数组并拆分每一行以创建具有其各自名称的图像的URL。但是没有连接,它什么也没做。

是否可以在PowerShell FTP中运行脚本以一次下载所有这些镜像?

我也尝试编写PHP脚本,但是遇到很多问题。这是代码:

<?php

$ftp_server = 'ftp.example.com';
$ftp_user_name = 'user';
$ftp_user_pass = 'pass';
$remoteFilePath = '/data/photos.txt.zip';
$localFilePath = $_SERVER['DOCUMENT_ROOT']."/path/";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if ((!$conn_id) || (!$login_result)) {
echo 'FTP connection has failed! Attempted to connect to '. $ftp_server. ' for user '.$ftp_user_name.'.';
}else{
echo 'FTP connection was a success.<br>';
$directory = ftp_nlist($conn_id,'');
echo '<pre>'.print_r($directory,true).'</pre>';

$contents = ftp_nlist($conn_id, "/data/");
var_dump($contents);

$bla = ftp_nlist($conn_id, "/photos/");
var_dump($bla);

ftp_pasv($conn_id, true);

if (ftp_get($conn_id, $localFilePath.'/photos.txt.zip', $remoteFilePath, FTP_BINARY)) {

echo "File has been downloaded!!";
return true;

} else {
echo "fail ... ";
echo "Connected has be stopped!!";
return false;
}
}
ftp_close($conn_id);

?>

这将连接到FTP,测试连接并将zip文件下载到本地计算机上。
<?php

// assuming file.zip is in the same directory as the executing script.
$file = 'photos.txt.zip';

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo " $file extracted to $path";
} else {
echo "Couldn't open $file";
}

?>

这将以前下载的zip文件夹解压缩到同一位置。尝试采用路径并下载图像的最后一段代码不起作用:

header("Content-Type: text/html; charset=UTF-8");    
$lines = file("photos.txt");
$dir = "local\\path\\to\\file\\";

foreach ($lines as $line) {
$parts = explode("\t", $line);
$something = $parts[1];
$somethingelse = $parts[0];
$var1 = $ftp_server.$something;

file_put_contents($dir, file_get_contents($var1));
}

它成功地拆分了数组并获取了URL,但是它没有下载图像,而是给了我错误:

file_get_contents (...) failed to open stream: No such file or directory in...

file_get_contents (...) failed to open stream: Permission denied in...

我曾尝试按照其他主题中的建议更改所有权限,但无济于事。

我还找到了类似的主题 here,但是提出的解决方案已过时,根本没有帮助。

我必须说我对PowerShell和PHP还是很陌生,希望您能提供任何帮助。

最佳答案

我最终使用了PHP。这是工作代码:

1-下载压缩文件

$ftp_server = 'ftp.example.com';
$ftp_user_name = 'user';
$ftp_user_pass = 'psasword';
$remoteFilePath = '/data/photos.txt.zip';
$localFilePath = $_SERVER['DOCUMENT_ROOT']."/images";


// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if ((!$conn_id) || (!$login_result)) {
echo 'FTP connection has failed! Attempted to connect to '. $ftp_server. ' for user '.$ftp_user_name.'.';
}else{
echo 'FTP connection was a success.<br>';
$directory = ftp_nlist($conn_id,'');
echo '<pre>'.print_r($directory,true).'</pre>';

$contents = ftp_nlist($conn_id, "/data/");
var_dump($contents);

$photos = ftp_nlist($conn_id, "/photos/");
var_dump($photos);


ftp_pasv($conn_id, true);

if (ftp_get($conn_id, $localFilePath.'/ddda-photos.txt.zip', $remoteFilePath, FTP_BINARY)) {

echo "File has been downloaded!!";
return true;

} else {
echo "fail ... ";
echo "Connected has be stopped!!";
return false;

}

}

2-解压缩文件
// assuming file.zip is in the same directory as the executing script.
$file = 'photos.txt.zip';

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "$file extracted to $path";
} else {
echo "Couldn't open $file";
}

3-阅读文本文件并下载图像
//So it doesn't time out
set_time_limit(0);

$ftp_server = 'ftp.example.com';
$ftp_user_name = 'user';
$ftp_user_pass = 'password';

$lines = file("photos.txt");
$dir = "F://test/";

//Goes through the file and separates name path and hash
foreach ($lines as $line) {
$parts = explode("\t", $line);
$imagePath = $parts[1];
$imageName = $parts[0];
$var1 = $ftp_server.trim($imagePath,'/');

$ftpUrl = 'ftp://'.$ftp_user_name.':'.$ftp_user_pass.'@'.$ftp_server.'/'.$imagePath;

// ftp url dump to be sure that something is happening
var_dump($ftpUrl);

$curl = curl_init();
$fh = fopen($dir.$imageName, 'w');
curl_setopt($curl, CURLOPT_URL, $ftpUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);

fwrite($fh, $result);
fclose($fh);
curl_close($curl);
}

问题在于,对于每个请求,cURL必须传递用户名和密码才能下载图片。

希望这对遇到同样问题的人有所帮助。

关于php - 从带有路径列表的FTP服务器下载文件并下载列出的每个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49738410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com