Jump to content

FTP Question--


makeshift_theory

Recommended Posts

Ok guys, really need your help on this one.  I am trying to create a function that will ftp an entire directory structure and all from a source.  Below is the code with the user/pass 'x' out.

[code]
<?

function copydirr($fromDir,$toDir,$chmod=0777,$p1,$verbose=false)
/*
  copies everything from directory $fromDir to directory $toDir
  and sets up files mode $chmod
*/
{
//* Check for some errors
$errors=array();
$messages=array();
$ftp_server = "ftp.startrekrpg.com";
$ftp_user_name = "******";
$ftp_user_pass = "************";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$site_ftpdirectory = "http://www.startrekrpg.com/";
if((!$login_result) || (!$conn_id))
$errors[] = "Could not connect to ftp or login";
else
echo "Connection Accepted.";


if(!ftp_chdir($conn_id, $site_ftpdirectory . $toDir) && $p1 != 1)
echo "Change Failed" . $site_ftpdirectory . $toDir . "<br><br>";
else
if(!ftp_chdir($conn_id, $site_ftpdirectory . $toDir))
echo "Change Failed on From" . $site_ftpdirectory . $toDir. "<br><br>";


//Error Handling
//if (!is_writable($toDir))
  // $errors[]='target '.$toDir.' is not writable'; // Source Directory is not writable
//if (!is_dir($toDir))
//  $errors[]='target '.$toDir.' is not a directory'; // Can't find source directory
if (!is_dir($fromDir))
  $errors[]='source '.$fromDir.' is not a directory'; // Can't find directory to ftp to
if (!empty($errors)) // If errors are present print them out
  {
  if ($verbose)
      foreach($errors as $err)
          echo '<strong>Error</strong>: '.$err.'<br />';
  return false;
  }
//*/
$exceptions=array('.','..');
//* Processing
$handle=opendir($fromDir);
while (false!==($item=readdir($handle)))
  if (!in_array($item,$exceptions))
      {
      //* cleanup for trailing slashes in directories destinations
      $fromftp=str_replace('//','/',$fromDir.'/'.$item);
      $toftp=str_replace('//','/',$to.'/'.$item);
      //*/
      //if (is_file($fromftp))
          // {
        // if (@ftp_put($conn_id, $toftp, $fromftp, FTP_ASCII)) // Copies From to To
            //  {
              //chmod($to,$chmod);
              // touch($to,filemtime($from)); // to track last modified time
            //  $messages[]='File copied from '.$fromftp.' to '.$toftp;
          //  }
          //else
              // $errors[]='cannot copy file from '.$fromftp.' to '.$toftp;
          // }
  // If Creates Directory
      if (is_dir($fromftp))
          {
          if (@ftp_mkdir($conn_id,$toftp))
              {
              //chmod($to,$chmod);
              $messages[]='Directory created: '.$toDir.$toftp;
              }
            else
              $errors[]='cannot create directory '.$toDir.$toftp;
          copydirr($fromftp,$toftp,$chmod,$p1=1,$verbose);
          }
 
      }
closedir($handle);
//*/
//* Output
if ($verbose)
  {
  foreach($errors as $err)
      echo '<strong>Error</strong>: '.$err.'<br />';
  foreach($messages as $msg)
      echo $msg.'<br />';
  }
//*/
return true;
}
$from = "cwillard/sites/BioPharm/";
$to = "custom";
copydirr($from,$to,$chmod=0777,$p1 = 0,true);
?>




[/code]

It will copy all the files to one folder and the folders to the root folder and it copies the right structure for the "website folder."
Link to comment
https://forums.phpfreaks.com/topic/23880-ftp-question/
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.