• PHP Downloading From FTP
    10 replies, posted
Hi, basically I have a Garry's Mod server and uploading the cache is a right pain in the ass, I need a PHP script that can download the cache from the game server and then place it in the correct folder on my webserver. If anyone can quickly whip me up a script that would be awesome.
[url]http://php.net/manual/en/book.curl.php[/url]
You use cURL to download the file: [url]http://net.tutsplus.com/tutorials/php/techniques-and-resources-for-mastering-curl/[/url] And copy() to move it around: [url]http://php.net/manual/en/function.copy.php[/url] It's pretty much all you need and it's really easy to learn, so you should do it yourself, you gain some knowledge that way too.
I need to connect to a FTP server though and download from that.
[b]franciscocha at gmail dot com[/b] [i]20-May-2009 01:11[/i] Example how to connect to FTPES (FTP explicit SSL). This script will connect to any FTPES server and out put the list of directories. [php] <?php $username = 'username'; $password = 'password'; $url = 'url'; $ftp_server = "ftp://" . $username . ":" . $password . "@" . $url; echo "Starting CURL.\n"; $ch = curl_init(); echo "Set CURL URL.\n"; //curl FTP curl_setopt($ch, CURLOPT_URL, $ftp_server); //For Debugging //curl_setopt($ch, CURLOPT_VERBOSE, TRUE); //SSL Settings curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); //List FTP files and directories curl_setopt($ch, CURLOPT_FTPLISTONLY, TRUE); //Output to curl_exec curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); echo "Executing CURL.\n"; $output = curl_exec($ch); curl_close($ch); echo "Closing CURL.\n"; echo $output . "\n"; $files = explode("\n", $output); print_r($files); ?> [/php] [url]http://php.net/manual/en/function.curl-setopt.php[/url]
I cant find the thing to download :S
[url]http://stackoverflow.com/questions/1178425/download-a-file-from-ftp-using-curl-and-php[/url]
I seriously cant get it to work....... can someone just write me the script and show me howto do it.
It's not that hard.
Let me get the example backend to my sprays page, it basically uses that system what you want. Settings.php (just holds the connection and path details) [php] // downloader and converter settings define("OUTDIR", "host/path/here"); /* change this to the directory the sprays are to be saved, please don't add a slash to the end of this. make sure this directory has permissions set, to 777 (read/write) to be exact. */ define("INDIR", "/89.145.94.47 port 27040/tf/downloads"); /* change this to the /downloads directory on the server's ftp, !!do not end with a slash!! You can figure this out by connecting to the FTP server and navigating to the /downloads directory it's a directory with a lot of .ztmp and .dat files*/ define("LOG", "log/log.txt"); /* Set this to whatever you think is best for this. Believe me, you'd hate having to wait for this script just to die somewhere. You need to create this file, and set it's permessions to 777 (read/write)*/ define("DELETEDAYS", "7"); // the amount of days sprays are to be saved. define("FORMAT", "gif"); // don't put a dot in here, bad idea. Just leave this as it is. // index settings define('THIS_SCRIPT', 'sprays_index'); // change depending on your page name define('CSRF_PROTECTION', true); // --MYSQL SETTINGS-- // // Set these to the SQL server you're using for the sprays. $user = ""; $pass = ""; $db = ""; $host = ""; // --FTP SETTINGS--// // If your server is on the same machine as your webhost, set use_ftp to false. // Otherwise you will need to set this to the FTP of your GAMEserver. $use_ftp = true; $ftp = ""; $ftpuser = ""; $ftpass = ""; [/php] Actual download script. [php] // fix retardation chdir(dirname(__FILE__)); // HELLO! IF YOUR INDEX.PHP IS NOT IN THE SAME FOLDER AS THIS FILE, PLEASE CHANGE THE FOLLOWING TO THE PATH WHERE SETTINGS.PHP IS LOCATED!!! include_once("../settings.php"); $stamp = "o-m-d H:i:s"; function get_ftp_mode($file) { $path_parts = pathinfo($file); if (!isset($path_parts['extension'])) return FTP_BINARY; switch (strtolower($path_parts['extension'])) { case 'am':case 'asp':case 'bat':case 'c':case 'cfm':case 'cgi':case 'conf': case 'cpp':case 'css':case 'dhtml':case 'diz':case 'h':case 'hpp':case 'htm': case 'html':case 'in':case 'inc':case 'js':case 'm4':case 'mak':case 'nfs': case 'nsi':case 'pas':case 'patch':case 'php':case 'php3':case 'php4':case 'php5': case 'phtml':case 'pl':case 'po':case 'py':case 'qmail':case 'sh':case 'shtml': case 'sql':case 'tcl':case 'tpl':case 'txt':case 'vbs':case 'xml':case 'xrc': return FTP_ASCII; } return FTP_BINARY; } /* And here comes my code. Rainbows make me cry. It might just be coding genocide, but it should be safe. */ ////// if($use_ftp == true) { $conn = ftp_connect($ftp); // Connect to the ftp, if (!$conn) { file_put_contents(LOG, "[".date($stamp)."] FTP connection failure!\n", FILE_APPEND); // log die("Couldn't connect to FTP server :("); // die on failure } if (!@ftp_login($conn, $ftpuser, $ftpass)) { // Login on the ftp stream, file_put_contents(LOG, "[".date($stamp)."] FTP login failure!\n", FILE_APPEND); //log die("Failed to login"); // die on failure } ftp_pasv($conn, true); // turn on passive mode, YEAH BABY TURN ME ON // sorry } // And this is where it's gonna get bad if($use_ftp == true) { $dirlist = ftp_nlist($conn, INDIR); // should return an array of files in 'INDIR', thus the sprays. }else{ $dirlist = glob(INDIR."*"); } $dirlistweb = glob("{".INDIR."/*.png,".INDIR."/*.gif}"); // Oh dear god, take cover. $download = array(); // first declare an array file_put_contents(LOG, "\n\n--Files downloading--:\n\n", FILE_APPEND); // indicate file downloading in progress mysql_connect($host, $user, $pass); // connect to sql mysql_select_db($db); // select db $res = mysql_query("select * from sprays where datediff(firstdate, NOW()) < ".DELETEDAYS." or (date > 0 and datediff(date, NOW()) < ".DELETEDAYS.")"); // select all sprays from 7 days and those that were not sprayed yet while($row = mysql_fetch_assoc($res)) { // fetch all spray entries in sql if ($use_ftp == true) { if(!file_exists(OUTDIR."/"."{$row['filename']}.".FORMAT)) { $err1 = ftp_get($conn, OUTDIR."/{$row['filename']}.vtf", INDIR."/{$row['filename']}.dat", get_ftp_mode($download[$i])); // download the vtf file $err2 = ftp_get($conn, OUTDIR."/{$row['filename']}.vtf.ztmp", INDIR."/{$row['filename']}.dat.ztmp", get_ftp_mode($download[$i])); // download the vtf.ztmp file } }else{ //copy } } if($err1 == true && $err2 == true) { file_put_contents(LOG, "[".date($stamp)."] File downloading done successfully!\n\n", FILE_APPEND); // indicate so. }else{ file_put_contents(LOG, "[".date($stamp)."] One or more files failed to download!\n\n", FILE_APPEND); // indicate so. } ftp_close($conn); // finally, [/php] It uses a mysql with the downloads but you should get the idea how to use it, its not the best way but it works.
I wrote a script once for this sole purpose, it also bz2'd all my files automatically. I'd literally type in /models/zombifiedworld/ and it'd download it all to the local cache with recursive folders etc, then bz2 everything. I'll see if I can find it for you.
Sorry, you need to Log In to post a reply to this thread.