Jump to content


Photo

FTP Question--


  • Please log in to reply
2 replies to this topic

#1 makeshift_theory

makeshift_theory
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts

Posted 13 October 2006 - 06:33 PM

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.

<?

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);
?>





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."
<b>Emacs</b> it's great for the body.
<br><br>
<b>Trust the TechnoLust</b>

<b><u>Have a question check here:</u></b>
PHP Manual: http://www.php.net

#2 kanikilu

kanikilu
  • Members
  • PipPip
  • Member
  • 29 posts
  • LocationDallas, TX

Posted 13 October 2006 - 09:34 PM

You'd probably get a better response here if you actually asked a question or told us what was wrong with the code you posted...

#3 makeshift_theory

makeshift_theory
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts

Posted 13 October 2006 - 11:06 PM

I thought I did but here goes again:

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."

Basically to reitterate it doesn't copy the folder structure correctly.
<b>Emacs</b> it's great for the body.
<br><br>
<b>Trust the TechnoLust</b>

<b><u>Have a question check here:</u></b>
PHP Manual: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users