通过 PHP 上传远程服务器文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12232605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remote Server File Upload Via PHP
提问by Milksnake12
I have two servers, one with my website, the other for storage. I'm trying to have a page where someone can upload a file to the storage server, I'm hoping to use a form post to get it there. I've written a very simple bit of code to troubleshoot this and am having a little trouble. It works fine if I change the action to a .php that saves it on the same server, but when I change it to my storage server, it fails to upload and shows me the "else" echo that my fail failed to upload.
我有两台服务器,一台用于我的网站,另一台用于存储。我试图有一个页面,有人可以在其中将文件上传到存储服务器,我希望使用表单帖子将其发送到那里。我写了一段非常简单的代码来解决这个问题,但遇到了一些麻烦。如果我将操作更改为将它保存在同一台服务器上的 .php,它可以正常工作,但是当我将其更改为我的存储服务器时,它无法上传并显示我无法上传的“其他”回声。
the HTML on my web server:
我的网络服务器上的 HTML:
<form action="http://storageServer/upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
The PHP on my storage server:
我的存储服务器上的 PHP:
<?php
$folder = "files/";
$path = $folder . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
The .php is in the html folder with the "files" folder.
.php 位于带有“files”文件夹的 html 文件夹中。
Any reason the file isn't making it to the server that you can see?
该文件没有发送到您可以看到的服务器的任何原因?
采纳答案by Sarah Lasonia
This topic answers your question
As suggested, you could use CURL:
按照建议,您可以使用 CURL:
$ch = curl_init("http://www.remotepage.com/upload.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileupload' => '@'.$_FILES['theFile']['tmp_name']));
echo curl_exec($ch);

