I found this very nice ftp script that allows uploading to a remote ftp server.

<form name="fileup" method="post" enctype="multipart/form-data" action="flatest_v.php">
<br><br><br>Upload new firmware file : <input type="file" size="30" name="userfiles[]"><br>
<br>
<!-- change below to your max -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000">
<input type="hidden" name="upload" value="upload">
<input type="submit" value="submit" name="submit">
</form>


<?
if (!empty($_REQUEST['upload'])) {

//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,".");
$p++;
If($length!=-1){
$ext = substr($file,$p,$length);
}
If($length==-1){
$ext = substr($file,$p);
}
$ext = strtolower($ext);
Return $ext;
}

//Not good practice, but here anyway
//change to suit your needs
//also some have to be set in the ini
//for this to correctly work

//2meg max
Ini_set("upload_max_filesize","100M");

//turn on file uploads
Ini_set("file_uploads","1");

//set your temp dir
Ini_set("upload_tmp_dir","/tmp");

//set post size large enough to accomidate
//3 100meg files and some overhead
Ini_set("post_max_size","180M");

?>

</p>
<?
//check to see if we have submited yet
If($_POST["submit"]!="submit"){
//not yet so lets make the form
?>

<?
}
//see if we have submited and that the files array has been set
If(($_POST["submit"]=="submit")&&(is_array($_FILES['userfiles']))){

$ftp_user_name="*****"; //change to ftp username
$ftp_user_pass="*****"; //change to ftp password
$ftp_server="******"; //change to ftp url
$ftp_dump_dir="/files"; //change to destination directory

//go through all the files
For($x=0;$x<count($_FILES['userfiles']['name']);$x++){

//now we do some file checking

//check to see if file is there
If($_FILES['userfiles']['name'][$x]!="none"){
//file has a name
//check filesize
If($_FILES['userfiles']['size'][$x]!=0){
//file is larger than 0 bytes
//Check to see if it is uploaded
If(is_uploaded_file($_FILES['userfiles']['tmp_name'][$x])){
//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";
Exit;
} else {
Echo "Connected to $ftp_server! <br>";
//set PASV mode
If(!ftp_pasv($conn_id,TRUE)){
Echo "Could not enter PASV mode!";
}
//rename to file#_date.Ext
$filename = $_FILES['userfiles']['name'][$x];
//$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), "\and";
} else {
//you want to know if it didn't work
Echo "Couldn't change directory\and";
}

//upload the file and let the user know what happened
If(ftp_put($conn_id,$filename,$_FILES['userfiles']['tmp_name'][$x],FTP_BINARY)){
Echo "File ".$_FILES['userfiles']['name'][$x]." was sent successfully<br>";
Echo "File was named ".$filename."<br>";
}else{
Echo "There was a problem sending file ".$_FILES['userfiles']['name'][$x]."<br>";;
}
}
// close the FTP stream
Ftp_close($conn_id);
}
Else echo"File was not uploaded!<br>";
}
}
Echo "<br>";

}//end for loop

}
//That's all folks!
}

2 questions...

1 - Is there a simpler / easier way to do this ?
2 - anyway to add some form of progress bar or feedback ?

The script is being run on one server, uploading to another server !

Thanks 🙂

    Easier? Not really. More readable and maintainable? Definitely.

    First off, get rid of all your unnecessary closing and opening php tags ?><?php. That would usually be any sinlge set of such tags where there is nothing (but whitespace) in between. In your case however, you also have redundant html tags, such as a </p> which either has no matching start tag, or if there is a matching <p> tag outside of the posted code your tags are in an invalid order (<p><form></p>). Validate your html code.

    There are various ways to increase the abstraction level. One simple way would be to create an ftp class. Upon class instantiation you create your connection. Then just call $ftp->upload($file) several times.
    But even if you do not choose to do this, at least move the code to open and close the ftp connection outside of the loop. I.e.

    open ftp connection.
    move to appropriate directory
    for (... $file...) {
    
    }
    close ftp connection
    

    You should definitely inspect the error element for each file and only upload it if it's ok.

    You should also either remove your missguiding comments and let the code speak for itself, or at the very least correct the comments (or code if the comments are correct and the code isn't). Two examples:

    //2meg max
    Ini_set("upload_max_filesize","100M"); 
    
    //set post size large enough to accomidate
    //3 100meg files and some overhead
    Ini_set("post_max_size","180M");
    

    Finally, convert all your function calls to lower cases. I have no idea why php is case sensitive for variables but not function names. So while your code is syntactically correct, at least I believe it's bad practice to not adhere to function name casing as it was defined.

      Hi

      As I posted... I found this on a web site, it's not my code !!

      I agree about removing the comments etc.

      Anyway.. is it possible to add either a progress bar or a text percentage gauge ??
      etc ?

      Thanks

        Sort of /depends.

        Javascript does not allow you to access the user's file system to check file size, so this is unknown before the file is uploaded to the server. But if you write to a file stream over ftp you can return information to the client about this progress.

        For the first "half" of the file transfer, you'd have to resort to something like flash or a java applet.

          Thanks johanafm.

          As this is FTP, is there any way to do a simple file size = xxx, uploaded = yyy and have that update ?

          If so.. how 🙂 ??

          Thanks

            Never done it, but have a look at:
            http://php.net/manual/en/wrappers.ftp.php lets you append to files which means you can write a specific amount of bytes at a time and keep track of this. Compare to local file size and you have your progress.
            Another way that might work is using ftp_nb_put (or ftp_nb_fput) and then keep checking the file size on the ftp server. But the server may preallocate the needed space and always report total file size for all I know. Or some ftp servers might.
            Then there is the undocumented start parameter for ftp_put/ftp_fput which may indicate staring position in target file, starting position in source file, or both. Test and see.

              Write a Reply...