【发布时间】:2018-09-19 04:11:39
【问题描述】:
我正在做一个项目,我需要从 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 还很陌生,如果你能提供任何帮助,我将不胜感激。
【问题讨论】:
-
使用 cURL 代替 file_get_contents 并下载图像的每一位,然后将其转储到目录中。几个月前做过类似的事情,但 cURL 是你的答案;)
-
我尝试使用以下代码代替 file_get_contents 行:$ch = curl_init($var1); $fp = fopen($dir, 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);没有错误,但它什么也没做
-
$url是什么样的? -
当 $url 打印出来时,它看起来就像这样 /photos/pvo/transfertvo/photos/pc33c1/pc33c1_x48_08_hd.jpg pc33c1_26126_08_hd.jpg
-
但这不是网址!所以如果你可以
get那个路径,我看不出是什么让你相信你应该使用FTP来检索文件,如果你可以直接获取它。
标签: php powershell ftp ftpwebrequest