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

时间:2018-04-09 17:24:38

标签: php powershell ftp ftpwebrequest

我正在处理一个项目,我需要从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命令时,可以下载图片,但只能逐个下载(并且需要下载数百张图片)。

我尝试编写一个经过.txt文件的PowerShell脚本,并且""查找图像所需的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));
}

它成功拆分了数组并获取了网址,但它并没有下载图片,反而给了我错误:

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都很陌生,我很感激您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

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

1-下载zip

$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必须传递用户和密码才能下载图片。

希望这可以帮助有同样问题的人。

答案 1 :(得分:0)

您似乎希望在使用FtpWebRequest“连接”到FTP后,get命令会以某种方式神奇地从FTP服务器获取文件。

这假设很多事情根本不正确,很难猜到从哪里开始解释。

  • PowerShell中没有get。这是FTP客户端的常用命令。
  • FtpWebRequest无法连接。该类旨在执行一次性作业,如下载文件等。不要创建持久连接。
  • 等等等。

基本上,您需要使用WebRequestFtpWebRequest)下载列表文件,然后对每个文件重复相同的操作:

$credentials = New-Object System.Net.NetworkCredential("username", "password") 

$baseUrl = "ftp://ftp.example.com"
$listPath = "/remote/path/list.txt"
$listUrl = $baseUrl + $listPath

Write-Host "Retrieving file list from $listPath..."

$listRequest = [Net.WebRequest]::Create($listUrl)
$listRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$listRequest.Credentials = $credentials

$lines = New-Object System.Collections.ArrayList

$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
    $line = $listReader.ReadLine()
    $lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()

foreach ($line in $lines)
{
    $fields = $line.Split("`t")
    $file = $fields[0]
    $remoteFilePath = $fields[1]

    Write-Host "Downloading $remoteFilePath..."
    $fileUrl = $baseUrl + $remoteFilePath
    $localFilePath = $file

    $downloadRequest = [Net.WebRequest]::Create($fileUrl)
    $downloadRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
    $downloadRequest.Credentials = $credentials

    $downloadResponse = $downloadRequest.GetResponse()
    $sourceStream = $downloadResponse.GetResponseStream()
    $targetStream = [System.IO.File]::Create($localFilePath)
    $buffer = New-Object byte[] 10240
    while (($read = $sourceStream.Read($buffer, 0, $buffer.Length)) -gt 0)
    {
        $targetStream.Write($buffer, 0, $read);
    }
    $targetStream.Dispose()
    $sourceStream.Dispose()
    $downloadResponse.Dispose()
}

代码与PowerShell FTP download files and subfolders的答案基本相同,只是它从文件中检索文件列表,而不是从目录列表中检索。