I'm working on a project at the moment where I'm required to download images from an ftp server in order to put them on a website.
The path to the images is located in a txt file which is zipped on the server. The path for each image looks like this:
pc33c1_26126_08_hd.jpg /photos/pvo/transfertvo/photos/pc33c1/pc33c1_x48_08_hd.jpg 1a71ffb90de7628b8b1585b7e85c68e7
The way to get the photos should look like this:
get /photos/pvo/transfertvo/photos/pc33c1/pc33c1_x48_08_hd.jpg pc33c1_26126_08_hd.jpg
This example was provided by the company that's hosting the images.
When I connect to the ftp using PowerShell, there are two directories available - one where the zip file is located, and another one that appears to be empty. When you look at the paths in .txt file, you can clearly see that the pictures are actually located inside the second directory that appears to be empty. When you use the get command in the PowerShell, you can download the picture, but only one by one (and there are hundreds of pictures that need to be downloaded).
I've tried writing a PowerShell script that goes through the .txt file and "takes" the URL needed to find the image. First I need to connect to the FTP, but the following code doesn't work:
$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
The second piece of code is supposed to go through the .txt file once I download it from the FTP but I'm unable to connect to the server to do that in the first place.
$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 ""
}
What this should do, it should go through the array and split each line to create the URL of the image with its respective name. But without the connection, it does nothing.
Is it possible to run a script inside PowerShell FTP to download all those images at once?
I have also tried writing a PHP script, but I have encountered many problems. Here's the code:
<?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);
?>
This connects to the FTP, tests the connection and downloads the zip file on the local computer.
<?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";
}
?>
This one unzips the previously downloaded zip folder at the same location. The final piece of code, which attempts to take the path and download the images, doesn't work:
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));
}
It successfully splits the array and gets the URL, but it doesn't download the images but instead gives me errors:
file_get_contents (...) failed to open stream: No such file or directory in...
file_get_contents (...) failed to open stream: Permission denied in...
I have tried changing all the permissions as proposed in some other topics, but to no avail.
I've also found a similar topic here, but the solution proposed was obsolete and not helpful at all.
I must say I'm pretty new to PowerShell and PHP, and I would appreciate any help that you can provide.