哪一个是更可靠的PHP ftp_connect vs file_put_content(ftp://).


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

在我的php restful api中运行在heroku dyno上,因为我的api接受用户上传文件view ($_FILES) -form post-我需要将上传的文件转发到我的ftp服务器以持久化它们,因为heroku没有存储。

我搜索了一下,找到了实现这一目标的最佳方法,发现了两种方法

代码示例显示两个方法

// 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');

我在我的FTP服务器上测试了这两种方法,两者都有效,但我担心可靠性,关于这两种方法如何工作的细节很少有文档,所以我不确定一些事情。

  1. 我可以使用file_put_content标志时,把ftp文件?如FILE_APPEND或LOCK_EX ?
  2. 如果上传到一个不存在的文件夹会发生什么?
  3. 哪种方法更可靠,更不容易出错?
  4. 虽然file_put_contents不那么冗长,但我们需要在写入之前先读取文件内容,这会导致内存问题吗?
  1. FTP URL包装器支持PHP 5的附加文件:
    https://www.php.net/manual/en/wrappers.ftp.php refsect1-wrappers.ftp-options
  2. 没有使用任何方法创建
  3. FTP文件夹。你必须自己处理这件事。
  4. 太过宽泛。
  5. 不清楚。