heroku - which one is more reliable in php ftp_connect vs file_put_content(ftp://) -
in php restful api running on heroku dyno, since api accept user upload files view ($_files) -form post- need forward uploaded files ftp server persist them since heroku not have storage.
i googled around find best way achieve , found 2 methods
code example show both methods
// common code: $tmp_file_path = $_files['image']['tmp_name']; $raw_filename = explode('.',$_files['image']['name']); $extention = array_pop($raw_filename); $filename = md5(implode('.',$raw_filename)).$extention; // 1st: ftp_put_content; $content = file_get_contents($tmp_file_path); // additional stream required per http://php.net/manual/en/function.file-put-contents.php#96217 $stream = stream_context_create(['ftp' => ['overwrite' => true]]); $upload_success = file_put_content('ftp://username:password@ftpserver.com/'+$filename, $content, 0, $stream); // 2nd: ftp_put $conn_id = ftp_connect("ftp.simpleinformatics.com"); $login_result = ftp_login($conn_id, "username", "password"); ftp_pasv($conn_id, true); //for somereason ftp serve requires ! if (!$conn_id or !$login_result) $upload_success = false; $ftp_upload_success = ftp_put($conn_id,'/'.$filename, $tmp_file_path); echo "file_put_content" . ($upload_success ? "upload success" : 'file upload failed'); echo "ftp_put" . ($ftp_upload_success ? "upload success" : 'file upload failed');
i test 2 methods against ftp server , both works yet worried reliability, there few documentation details of how 2 methods works i'm not sure of couple things.
- can use file_put_content flags when putting ftp file ? file_append or lock_ex ?
- what happen if uploading folder not yet exists ?
- which method more reliable , less prone fail ?
- although file_put_contents less verbose, need read file content first before writing, can cause memory problem ?
- ftp url wrapper supports appending of php 5:
https://secure.php.net/manual/en/wrappers.ftp.php#refsect1-wrappers.ftp-options - ftp folder not created, using method. have take care of yourself.
- too broad.
- unclear.
Comments
Post a Comment