Remote Server File Upload Via PHP

2020-03-01 20:02发布

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?

标签: php file post
1条回答
啃猪蹄的小仙女
2楼-- · 2020-03-01 20:27

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);
查看更多
登录 后发表回答