如何在 slim3 中将文件上传到另一台服务器 php
How to upload a file into another server in slim3 php
我使用 silm3 作为我的 REST API 框架,并遵循 this 从 API 编写我的文件上传脚本。
我目前 运行 我的网络应用程序在 a.xyz.com 中,我的 REST API 在 b.xyz.com 中。现在我想从b.xyz.com/upload上传一张图片到a.xyz.com/uploaded/。
$newfile->moveTo("/path/to/$uploadFileName");
用于保存当前子域的文件。但我未能将其上传到另一个子域。
我在网上搜索,但没有找到任何线索。
将 b.xyz.com 上的文件移动到正确位置后,执行以下操作:
// Server B
// Move file to correct position on B
$newfile->moveTo("/path/to/$uploadFileName");
// Send file to A
$request = curl_init('a.xyz.com/upload');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, [
'file' => new CURLFile("/path/to/$uploadFileName")
]);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);
a.xyz.com 然后应该接受该文件并继续存储它。如果使用 Slim,与 b.xyz.com 一样
...
$newfile->moveTo("/path/to/$uploadFileName");
或使用PHP的核心功能
// Server A
if ( !empty($_FILES["file"]) && !empty($_FILES["file"]["tmp_name"]) ) {
// Move file to correct position on A
$uploadFileName = basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], "/path/to/$uploadFileName");
}
通常域或子域被限制在自己的网站空间内,因此您不能读取或写入其他域的文件。设置是calles open_basedir.
您必须在您的虚拟主机配置中设置它,但也有其他方法,例如在您的托管商允许的情况下使用 .htacces 文件进行设置:
How can I relax PHP's open_basedir restriction?
我使用 silm3 作为我的 REST API 框架,并遵循 this 从 API 编写我的文件上传脚本。
我目前 运行 我的网络应用程序在 a.xyz.com 中,我的 REST API 在 b.xyz.com 中。现在我想从b.xyz.com/upload上传一张图片到a.xyz.com/uploaded/。
$newfile->moveTo("/path/to/$uploadFileName");
用于保存当前子域的文件。但我未能将其上传到另一个子域。
我在网上搜索,但没有找到任何线索。
将 b.xyz.com 上的文件移动到正确位置后,执行以下操作:
// Server B
// Move file to correct position on B
$newfile->moveTo("/path/to/$uploadFileName");
// Send file to A
$request = curl_init('a.xyz.com/upload');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, [
'file' => new CURLFile("/path/to/$uploadFileName")
]);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);
a.xyz.com 然后应该接受该文件并继续存储它。如果使用 Slim,与 b.xyz.com 一样
...
$newfile->moveTo("/path/to/$uploadFileName");
或使用PHP的核心功能
// Server A
if ( !empty($_FILES["file"]) && !empty($_FILES["file"]["tmp_name"]) ) {
// Move file to correct position on A
$uploadFileName = basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], "/path/to/$uploadFileName");
}
通常域或子域被限制在自己的网站空间内,因此您不能读取或写入其他域的文件。设置是calles open_basedir.
您必须在您的虚拟主机配置中设置它,但也有其他方法,例如在您的托管商允许的情况下使用 .htacces 文件进行设置: How can I relax PHP's open_basedir restriction?