upgrader Posted July 8, 2008 Share Posted July 8, 2008 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 More sharing options...
jkewlo Posted July 8, 2008 Share Posted July 8, 2008 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 Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-584836 Share on other sites More sharing options...
upgrader Posted July 8, 2008 Author Share Posted July 8, 2008 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 https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-584844 Share on other sites More sharing options...
lemmin Posted July 8, 2008 Share Posted July 8, 2008 if (file_exists($file)) ? Or put all the file names into a table and check the table whenever you download it. Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-584848 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 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 More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 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 https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585390 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 I would rather do it with PHP first, so it can be used for people without SSH access. Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585399 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 but wget will do it over ftp too for the echo question, did you try echo "$file already exists\n<br />" ; Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585445 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 I will look into Rsync when I have this script sorted, as im interested in doing it with PHP anyway I tried that but it doesn't work, the output is like this: ./filename.ext already exists/n<br />- Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585455 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 sorry, didn't think about this being console output, so ditch the br. The line break isn't working because you are using /n instead of \n. copy and paste this: echo "$file already exists\n" ; Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585459 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 Thank you that works Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585469 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 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 https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585646 Share on other sites More sharing options...
lemmin Posted July 9, 2008 Share Posted July 9, 2008 It doesn't work in the browser because the php script needs to be parsed by a server running php. If you have such a server on your local computer you can put it in the right place and use http://localhost to test it locally. Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585678 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 Ok. Is there anyway I could write a script which would execute the download script when I open it in a browser? Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585732 Share on other sites More sharing options...
lemmin Posted July 9, 2008 Share Posted July 9, 2008 You could have a regular HTML file with some javascript that changes the page: <html> <body onload="self.location='http://server.com/download.php'"> </body> </html> Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585775 Share on other sites More sharing options...
upgrader Posted July 9, 2008 Author Share Posted July 9, 2008 Surely thats the same thing as directly opening the page?? Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-585868 Share on other sites More sharing options...
lemmin Posted July 10, 2008 Share Posted July 10, 2008 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 https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-586403 Share on other sites More sharing options...
upgrader Posted July 10, 2008 Author Share Posted July 10, 2008 I will give it a try.. Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-586497 Share on other sites More sharing options...
upgrader Posted July 10, 2008 Author Share Posted July 10, 2008 Didn't work. Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-586545 Share on other sites More sharing options...
lemmin Posted July 10, 2008 Share Posted July 10, 2008 Could you explain more about what you are trying to do? Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-586659 Share on other sites More sharing options...
upgrader Posted July 10, 2008 Author Share Posted July 10, 2008 Have it so when I open the download.php page containing the script in the post above it executes it and downloads the files rather than me running the command php download.php in SSH. Link to comment https://forums.phpfreaks.com/topic/113810-ftp-download-script/#findComment-586882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.