FTP stream loaded into chunks using PHP?

Is it possible to transfer FTP upload using PHP? I have files that need to be uploaded to another server, and I can only access this server via FTP. Unfortunately, I cannot configure the timeout on this server. Is it possible to do this?

Basically, if there is a way to write part of the file, and then add the next part (and repeat) instead of immediately loading all of this, it would save me. However, my googling did not give me an answer.

Is this achievable?

+2
source share
2 answers

OK, then ... It may be what you are looking for. Do you know curl?

CURL can support add-on for FTP:

curl_setopt ($ ch, CURLOPT_FTPAPPEND, TRUE); // APPEND FLAG

Another option is to use ftp: /// ftps: // streams, since they allow you to add PHP 5. See Ftp: //; ftps: // Documents. It may be easier to access.

+1
source

The easiest way to add a block to the end of a deleted file is to use file_put_contents with the FILE_APPEND flag:

 file_put_contents('ftp://username:pa‌​ ssword@hostname /path/to/file', $chunk, FILE_APPEND); 

If this does not work, perhaps because you do not have URL wrappers enabled in PHP .


If you need more control over the recording (transfer mode, passive mode, etc.) or you cannot use file_put_contents , use ftp_fput with the ftp_fput descriptor php://temp (or php://memory ) :

 $conn_id = ftp_connect('hostname'); ftp_login($conn_id, 'username', 'password'); ftp_pasv($conn_id, true); $h = fopen('php://temp', 'r+'); fwrite($h, $chunk); rewind($h); // prevent ftp_fput from seeking local "file" ($h) ftp_set_option($conn_id, FTP_AUTOSEEK, false); $remote_path = '/path/to/file'; $size = ftp_size($conn_id, $remote_path); $r = ftp_fput($conn_id, $remote_path, $h, FTP_BINARY, $size); fclose($h); ftp_close($conn_id); 

(add error handling)

0
source

All Articles