#native_company# #native_desc#

FTP File Upload

By Maxstropholis
on June 4, 2004

Version: 1.0

Type: Full Script

Category: File Management

License: GNU General Public License

Description: This simple script takes user files from a form and uploads them to an FTP site with a friendly name. Uses the PHP FTP functions. Also has some error checking.

//uses $_FILES[] global array 
//see manual for older PHP version info 

//This function will be used to get the extension from the filename 
function get_extension($file,$length=-1){ 
$p = strrpos($file,"."); 
  $ext = substr($file,$p,$length); 
  $ext = substr($file,$p); 
$ext = strtolower($ext); 
return $ext; 

//Not good practice, but here anyway 
//change to suit your needs 

//2meg max 

//turn on file uploads 

//set your temp dir 

//set post size large enough to accomidate 
//3 2meg files and some overhead 

  <title>ftp connect and upload</title> 
//check to see if we have submited yet 
//not yet so lets make the form 
<p>Upload Files to ftp site (2M MAX)</p> 
<form name="fileup" method="post" enctype="multipart/form-data" action="<? echo $PHP_SELF; ?>"> 
<input type="file" name="userfiles[]"><br> 
<input type="file" name="userfiles[]"><br> 
<input type="file" name="userfiles[]"><br> 
<!-- change below to your max --> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
<input type="submit" value="submit" name="submit"> 
//see if we have submited and that the files array has been set 

$ftp_user_name="ftpuser"; //change to ftp username 
$ftp_user_pass="ftppass"; //change to ftp password 
$ftp_server="yoursitehere.com"; //change to ftp url 
$ftp_dump_dir="/destination"; //change to destination directory 

//go through all the files 

  //now we do some file checking 

  //check to see if file it there 
  //file has a name 
  //check filesize 
        //file is larger than 0 bytes 
          //Check to see if it is uploaded 
               //file has been uploaded! 
             //let the user know their file has be uploaded 
             echo "file ".$_FILES['userfiles']['name'][$x]." uploaded!<br>"; 
             //conect to ftp server 
             $conn_id = ftp_connect($ftp_server); 
            // login with username and password 
              $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
            // check connection 
             if ((!$conn_id) || (!$login_result)) { 
              echo "FTP connection has failed!<br>"; 
              echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
            } else { 
                 echo "Connected to $ftp_server! <br>"; 
                 //set PASV mode 
                     echo "Could not enter PASV mode!"; 
                 //rename to file#_date.ext 
                 $filename = "file".($x+1)."_".date("MdY"); 
                 $filename.= ".".get_extension($_FILES['userfiles']['name'][$x],3); 

                 //change directory 
                 if (@ftp_chdir($conn_id, $ftp_dump_dir)) { 
                     //maybe you want to make sure we are in the correct directory 
                    echo "Current directory is now : ", ftp_pwd($conn_id), "n"; 
                } else { 
                  //you want to know if it didn't work 
                  echo "Couldn't change directoryn"; 

                  //upload the file and let the user know what happened 
                       echo "File ".$_FILES['userfiles']['name'][$x]." was sent successfully<br>"; 
                    echo "File was named ".$filename."<br>"; 
                       echo "There was a problem sending file ".$_FILES['userfiles']['name'][$x]."<br>";; 
                // close the FTP stream 
            else echo"File was not uploaded!<br>"; 
    echo "<br>"; 

}//end for loop 

//That's all folks!