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
Share on other sites

make it create a backup folder. or have it make a new folder that add's the data in there? that's what i would do

 

How would I go about that, and is there not any other way? As I would rather have all the files in the same directory.

Link to comment
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
Share on other sites

since you are using cron, you must be on linux.  why not use wget with the --mirror option instead?  or better yet, if you have ssh access just use rsync.  Both would literally turn all of that code into a single line in your crontab

Link to comment
Share on other sites

Another Question:

 

When I run this script using: php ftpdl.php in SSH it works but if I just open it in my browser it doesn't work. Is there a way to get it working via a browser, it worked on one host I have but not the other -- not sure why.

Link to comment
Share on other sites

Directly openning the page will only use the browser to parse the client side code. When you direct it to a different server it can run the php. It would be the same as just making a .url shortcut file, really. Maybe I'm not sure what you are trying to do.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.