Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote Server File Upload Via PHP

Tags:

file

post

php

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?

like image 274
Milksnake12 Avatar asked Sep 02 '12 00:09

Milksnake12


1 Answers

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);
like image 122
Sarah Lasonia Avatar answered Nov 09 '22 17:11

Sarah Lasonia