通过PHP远程服务器文件上传 [英] Remote Server File Upload Via PHP

查看:390
本文介绍了通过PHP远程服务器文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台服务器,一台用于我的网站,另一台用于存储.我正在尝试创建一个页面,有人可以将文件上传到存储服务器,我希望使用表单发布将其发送到那里.我编写了一些非常简单的代码来解决此问题,但遇到了一些麻烦.如果将操作更改为将其保存到同一服务器上的.php,它会很好地工作,但是当我将其更改到存储服务器上时,它无法上传,并向我显示失败失败的其他"回显. /p>

我的Web服务器上的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>

存储服务器上的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!";
}
?>

.php位于带有文件"文件夹的html文件夹中.

任何原因导致文件无法发送到您可以看到的服务器上?

解决方案

此主题可以回答您的问题

根据建议,您可以使用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);

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.

the HTML on my web server:

<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
$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.

Any reason the file isn't making it to the server that you can see?

解决方案

This topic answers your question

As suggested, you could use 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);

这篇关于通过PHP远程服务器文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆