Jump to content


Web Site Design, Usability, SEO & Marketing Discussion and Support

Sign in to follow this  

Php Ftp - Append To An Existing File

Recommended Posts

Is it possible to append to an existing file using the built in PHP FTP?

I'm writing a script to upload large files split up into chunks - and want to reattach those chunks together, to finish with the large file I started with.




Share this post

Link to post
Share on other sites

Not really, but you can hack your way around it.


1. Crazy idea: Some FTP servers (so the destination server) support resuming. I wonder, crazily, if your uploading client can "resume" the large file on the server but actually be sending it the chunks.


2. Definitely will work idea: you need the "cat" (concatenation) command on unix systems. On Windows, you can use various equivalents.


So... what exactly is your set up?

Share this post

Link to post
Share on other sites

Thanks for your ideas eKstreme, I eventually got this to work:



function ftpupload( $server , $username , $password , $local_file , $remote_file ){$dest = "ftp://".$username.":".$password."@".$server."/".$remote_file;$ch = curl_init();$fp = fopen($local_file,"r");curl_setopt($ch, CURLOPT_UPLOAD, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 300);curl_setopt($ch, CURLE_OPERATION_TIMEOUTED, 300);curl_setopt($ch, CURLOPT_URL, $dest);curl_setopt($ch, CURLOPT_FTPAPPEND, TRUE ); // APPEND FLAGcurl_setopt($ch, CURLOPT_INFILE, $fp);curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));curl_exec($ch);fclose ($fp);$errorMsg = '';$errorMsg = curl_error($ch);$errorNumber = curl_errno($ch);curl_close($ch);return $errorNumber;}



Tried getting this to work - probably possible with a little more time:



file_put_contents("ftp://".$username.":".$password."@".$server."/".$local_file, file_get_contents( $local_file ), FILE_APPEND , $context );



I'm using this for a Java Applet that splits up a movie file into 100kb files and posts them to a PHP script. This then uses the above function to append the parts back together over FTP. Doing this keeps within the http POST upload_max_filesize and allows error checking throughout the upload.


Plus it's quite cool! :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this