Jump to content

Cre8asiteforums Internet Marketing
and Conversion Web Design


Photo

Php Ftp - Append To An Existing File


  • Please log in to reply
2 replies to this topic

#1 chrisbabar

chrisbabar

    Ready To Fly Member

  • Members
  • 19 posts

Posted 25 September 2008 - 04:50 AM

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.

Regards,
Chris

#2 eKstreme

eKstreme

    Hall of Fame

  • Hall Of Fame
  • 3400 posts

Posted 25 September 2008 - 04:59 AM

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?

#3 chrisbabar

chrisbabar

    Ready To Fly Member

  • Members
  • 19 posts

Posted 25 September 2008 - 07:06 AM

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 FLAG
	curl_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! :)



RSS Feed

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users