makeshift_theory Posted October 16, 2006 Share Posted October 16, 2006 Ok, after reviewing the forums for a place to put this I believe this is the right home for it. I have worked for a week on trying to develop a function that would allow the user to select a local directory and copy all subdirectories and files and chmod them correctly to a remote location. I searched countless places looking for this script and I could not find one that would do ALL the subdirectories. So discovering that I was going to have to "re-invent the wheel" if you know what I mean I spent a few days on this function. Well I finally have finished it and have added error messaging that you can turn on or off. I also placed the ftp login variables in the function so that way if you are pulling these variables from a db or flat-file you can do so. Comments are always welcomed as is constructive criticism...thanksPS> I have comments in there I used for testing, figured they may be useful if all hell breaks loose so I left them there.[code]<?/* FTP Upload Script Variable Definition: $detail if turned on (with any variable passed to it) will display all error messages and system messages Psuedocode: Grab All Files from local Create First Folder Name Change Directory to Created Check if File or Folder -> If File Upload to Directory -> If Folder Create Folders and cycle back through process with this name as the "FROM" ### Written By: Christopher Willard ### ########### Date: 10/16/06 ############ */function ftp_dwn($fromDir,$toDir,$chmod,$server,$user,$pass,$detail) {$ftp_server = $server;$ftp_user_name = $user;$ftp_user_pass = $pass;$conn_id = ftp_connect($ftp_server);$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);if((!$conn_id) || (!$login_result)) { echo "Could not Connect to the FTP Server"; exit();} ftp_mkdir($conn_id, $toDir); ftp_chmod($conn_id,$chmod,$toDir); ftp_chdir($conn_id,$toDir); // First run this will be custom///echo ftp_pwd($conn_id); // Test to see the current ftp directory;// Initialize Exception Array$exceptions = array('.','..'); if ($handle = opendir($fromDir)) { while (false !== ($file = readdir($handle))) if (!in_array($file,$exceptions)) { // handle slash exceptions $from=str_replace('//','/',$fromDir.'/'.$file); if (is_file($from)) { // If local file is a file handle here //echo $toDir . "/" . $file . "<br><br>"; if(!ftp_put($conn_id,$file,$from,FTP_ASCII)) $error[] = "Could not upload " . $toDir . "/" . $file . " correctly<br><br>"; if(!ftp_chmod($conn_id,$chmod,$file)) $error[] = "Could not chmod " . $toDir . "/" . $file . " successfully.<br><br>"; } if(is_dir($from)) { // If local file is a folder handle here //echo $toDir . "/" . $file; ftp_dwn($from,$toDir . "/" . $file,$chmod,$ftp_server,$ftp_user_name,$ftp_user_pass); // $toDir = custom/css } } // End Exception IF closedir($handle); } else $error[] = "Cannot Find Source Folder!"; // End Directory Handling if($detail) { // If Error Detail Is Turned On foreach ($error as $err) echo "<font color=red><b>" . $err . "</b></font>"; if(!$error) { echo "<font color=navy><b>FTP Structure Uploaded Successfully.</b></font>"; // Stop Script Execution }}} // End Function// Initialize FTP Server$ftp_server = "ftp.test.com";$ftp_user_name = "***********";$ftp_user_pass = "**********";ftp_dwn($fromDir = "cwillard/sites/Athena",$toDir = "customAthena",$chmod = 0777,$ftp_server,$ftp_user_name,$ftp_user_pass,$detail = 1);?>[/code] Link to comment https://forums.phpfreaks.com/topic/24127-ftp-entire-site-revisited/ Share on other sites More sharing options...
makeshift_theory Posted October 16, 2006 Author Share Posted October 16, 2006 I have also just completed the LEECH version (Grabs all files and structure from a remote site) Please see below:[code]<?/* FTP Download Script Variable Definition: $detail if turned on (with any variable passed to it) will display all error messages and system messages ### Written By: Christopher Willard ### ########### Date: 10/16/06 ############ */function ftp_dwn($server,$user,$pass,$fromDir,$toDir) {$chmod = 0777;$ftp_server = $server;$ftp_user_name = $user;$ftp_user_pass = $pass;$conn_id = ftp_connect($ftp_server);$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if((!$conn_id) || (!$login_result)) { echo "Could not Connect to the FTP Server"; exit(); } if(!file_exists($toDir)) // If our local directory doesn't exists let's create it mkdir($toDir,$chmod);$contents = ftp_nlist($conn_id, $fromDir); // Compiles a array of all given folders in remote structureforeach ($contents as $file) { if(ftp_size($conn_id,$file) != -1) { // Determine which files are folders and which are files $from = explode("/",$file); $fcount = count($from); $fcount--; ftp_get($conn_id,$toDir . "/" . $from[$fcount],$file,FTP_ASCII); } else { // File is a Folder $to = explode("/",$file); mkdir($toDir . "/" . $to[1],$chmod); ftp_dwn($ftp_server,$ftp_user_name,$ftp_user_pass,$file,$toDir . "/" . $to[1]); } }}// Initialize FTP Server$ftp_server = "ftp.test.com";$ftp_user_name = "**************";$ftp_user_pass = "**********";$fromDir = "customAthena";$toDir = "cwillard/sites/DownloadAthena";ftp_dwn($ftp_server,$ftp_user_name,$ftp_user_pass,$fromDir,$toDir);?>[/code] Link to comment https://forums.phpfreaks.com/topic/24127-ftp-entire-site-revisited/#findComment-109721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.