0

I've sent some post data with cURL and am now trying to send a file, getting lost along the way. I'm using a form with a file input. Then would like cURL to send it off to my server. Here is my upload page. <form action="curl_page.php" method="post" enctype="multipart/form-data"> Filename: <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>

Here is my curl page with some issues.

<?php
$tmpfile = $_FILES['file']['tmp_name'];
$filename = basename($_FILES['file']['name']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://my_server/file_catch.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
    'uploaded_file' => '@'.$tmpfile.';filename='.$filename,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
?>

The file_catch.php on my server looks like this.

<?php
$folder = "audio/";

$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!";
}
?>

Thanks for any help!

Milksnake12
  • 551
  • 1
  • 9
  • 19
  • Provide more information. Allow errors, show curl error functions traces, full reqeust trace that your target page gets – Ranty Dec 03 '12 at 19:40
  • Not sure how to go about doing that. Just looking at the code alone, any obvious errors? – Milksnake12 Dec 03 '12 at 21:09
  • Errors `error_reporting(E_ALL); ini_set('display_errors','On');` at start of each script; Curl erros `echo curl_error($curl) . ': ' . curl_errno($curl);` after `curl_exec` but before `curl_close`; you also likely want to see `print_r($_POST); print_r($_FILES);` in second script. – Ranty Dec 04 '12 at 09:00

1 Answers1

2

You are not sending the file that you got through the post request. Use the below code for posting the file through CURL request.

$tmpfile = $_FILES['image']['tmp_name'];
$filename = basename($_FILES['image']['name']);

$data = array(
    'uploaded_file' => '@'.$tmpfile.';filename='.$filename,
);

$ch = curl_init();   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// set your other cURL options here (url, etc.)

curl_exec($ch);

For more reference check the below link Send file via cURL from form POST in PHP

Community
  • 1
  • 1
Nishant
  • 3,614
  • 1
  • 20
  • 26