简体   繁体   English

哪一个在php ftp_connect vs file_put_content(ftp://)中更可靠

[英]which one is more reliable in php ftp_connect vs file_put_content(ftp://)

in my php restful api running on a heroku dyno, since my api accept user to upload files view ($_FILES) -form post- i need to forward the uploaded files to my ftp server to persist them since heroku does not have storage. 在我的php restful api上运行heroku dyno,因为我的api接受用户上传文件视图($ _FILES)-form post-我需要将上传的文件转发到我的ftp服务器以保留它们,因为heroku没有存储空间。

i googled around to find best way to achieve this and found two 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 is required as 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 my ftp serve requires this !
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 the two methods against my ftp server and both works yet i worried about reliability, there is very few documentation about details of how two methods works so i'm not sure of couple things. 我对我的ftp服务器测试这两种方法,两种方法都有效但我担心可靠性,关于两种方法如何工作的细节的文档很少,所以我不确定几件事情。

  1. can i use file_put_content flags when putting to ftp file ? 放到ftp文件时我可以使用file_put_content标志吗? like FILE_APPEND or LOCK_EX ? 喜欢FILE_APPEND还是LOCK_EX?
  2. what will happen if uploading to a folder that does not yet exists ? 如果上传到尚不存在的文件夹会发生什么?
  3. which method is more reliable and less prone to fail ? 哪种方法更可靠,更不容易失败?
  4. although file_put_contents is less verbose, we need to read file content first before writing, can this cause a memory problem ? 虽然file_put_contents的详细程度较低,但我们需要在写入之前先读取文件内容,这会导致内存问题吗?
  1. FTP URL wrapper supports appending as of PHP 5: FTP URL包装器支持从PHP 5开始追加:
    https://www.php.net/manual/en/wrappers.ftp.php#refsect1-wrappers.ftp-options https://www.php.net/manual/en/wrappers.ftp.php#refsect1-wrappers.ftp-options
  2. FTP folder is not created, using any method. 使用任何方法都不会创建FTP文件夹。 You have to take care of that yourself. 你必须自己照顾好自己。
  3. Too broad. 太宽泛。
  4. Unclear. 不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM