Jump to content

FTP Download Script


upgrader

Recommended Posts

Hi I have this script which connects to a game server and downloads all the files in one directory, I have it on a cron job so it downloads them every day. But the problem is it downloads and overwrites all the files it has already downloaded, which consumes lots of bandwidth. Is there anyway I can make it only download files it hasn't downloaded before?

 

<?php
$ftp_server = "";
$uname = ""; //Enter your ftp username here.
$pwd = ""; //Enter your ftp password here.
$directory = ""; //Enter the dir of the files on the server here.

$connection = ftp_connect($ftp_server) or die("Error connecting to $ftp_server");
$login = ftp_login($connection, $uname, $pwd);
if (ftp_chdir($connection, $directory)) {
    echo "Changed directory to: " . ftp_pwd($connection) . "\n";
} else {
    echo "Error while changing directory!\n";
}

ftp_pasv($connection,FALSE);

$files = ftp_nlist($connection, ".");
foreach ($files as $file)
{
   $newFile = fopen($file, 'w');
echo ftp_fget($connection,$newFile,$file,FTP_BINARY);
   fclose($newFile);
}



ftp_close($connection);

?>

 

Thanks for any help,

 

upgrader

 

 

Link to comment
https://forums.phpfreaks.com/topic/113810-ftp-download-script/
Share on other sites

Ok I have used if file exists. This is my new code:

 

<?php
$ftp_server = "";
$uname = ""; //Enter your ftp username here.
$pwd = ""; //Enter your ftp password here.
$directory = ""; //Enter the dir of the files on the server here.

$connection = ftp_connect($ftp_server) or die("Error connecting to $ftp_server");
$login = ftp_login($connection, $uname, $pwd);
if (ftp_chdir($connection, $directory)) {
    echo "Changed directory to: " . ftp_pwd($connection) . "\n";
} else { 
    echo "Error while changing directory!\n";
}

ftp_pasv($connection,FALSE);

$files = ftp_nlist($connection, ".");
foreach ($files as $file)
if (file_exists($file)) {
echo "$file already exists" ;
} else {
   $newFile = fopen($file, 'w');
   echo ftp_fget($connection,$newFile,$file,FTP_BINARY);
   fclose($newFile);
}



ftp_close($connection);

?>

 

How can I make the echo of existing files appear on separate lines, currently it spams it all as one line, I tried <br /> after the echo but it broke the script.

 

Thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585374
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.