在我的php restful api上运行heroku dyno,因为我的api接受用户上传文件视图($ _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:[email protected]/'+$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服务器测试这两种方法,两种方法都有效但我担心可靠性,关于两种方法如何工作的细节的文档很少,所以我不确定几件事情。