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!

3
  • Provide more information. Allow errors, show curl error functions traces, full reqeust trace that your target page gets Commented Dec 3, 2012 at 19:40
  • Not sure how to go about doing that. Just looking at the code alone, any obvious errors? Commented Dec 3, 2012 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. Commented Dec 4, 2012 at 9:00

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I changed my code to what you said and edited my post above. File still didn't make it across to the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.