0

I want to send file from local to remote server and after saving file to server I want to output the response. I am using cURL to send and upload file. It is working when I try it on local but not on remote server. I use sftp protocol with public authentication key for connection. what I need to change to send file to server.

Here is my code.

$target_url = 'https://example.com/accept.php';
$file_name_with_full_path = realpath('ss.zip');
$post = array('file' => new CurlFile($file_name_with_full_path, 'application/zip' /* MIME-Type */, 'ss.zip'));

    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
3
  • and whats the error? Commented Aug 28, 2017 at 8:05
  • there isn't any error. it just show blank page. Commented Aug 28, 2017 at 8:10
  • check logs then, blank page is usualy error 500 Commented Aug 28, 2017 at 8:11

1 Answer 1

5

If you want to upload images to an external server which is uploaded to your site by a client, you are at the right tutorial.

For this submission we will use 2 files :

  • form.php – The Page Where we will show the client the form. This file also sends the uploaded data to the external server.

  • handle.php – The Page on the external server which receives the uploaded data from form.php using cURL.

We won’t copy the uploaded file by the client to our server, instead we will directly send the file to the external server. For sending we will encrypt the file with base64. OK. Lets’ Start. First, Let’s create the FORM page :

<form enctype="multipart/form-data" encoding='multipart/form-data' method='post' action="form.php">
  <input name="uploadedfile" type="file" value="choose">
  <input type="submit" value="Upload">
</form>
<?
if ( isset($_FILES['uploadedfile']) ) {
 $filename  = $_FILES['uploadedfile']['tmp_name'];
 $handle    = fopen($filename, "r");
 $data      = fread($handle, filesize($filename));
 $POST_DATA = array(
   'file' => base64_encode($data)
 );
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, 'http://extserver.com/handle.php');
 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
 curl_setopt($curl, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
 $response = curl_exec($curl);
 curl_close ($curl);
 echo "<h2>File Uploaded</h2>";
}
?>
Now the code of the handle.php in external server where we sent the data using cURL :

$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
/* Now you can copy the uploaded file to your server. */
file_put_contents('subins', $decoded_file);
The above code will receive the base64 encoded file and it will decode and put the image to its server folder. This might come in handy when you want to have your own user file storage system. This trick is used by ImgUr and other file hosting services like Google.
Sign up to request clarification or add additional context in comments.

1 Comment

thank you o// i was trying to send the file direct to a repository via curl and it wasn't working. So, i read this post and changed my strategy, i send to another php file to treat the image, and then, save the file, now it's working o///

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.