1

This is my curl initiation code:

 $target="url/curlUploadHandler.php"; $args = new CURLFile('down.png', 'image/png','test'); //file to post $postFields = array('file' => $args); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $target); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data')); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl,CURLOPT_SAFE_UPLOAD, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields); $r = curl_exec($curl); curl_close($curl); print_r($r); // to get response from the page 

And this is to check whether given file is getting posted or not:

curlUploadHandler.php

 if($_REQUEST){ var_dump($_REQUEST); } 

or

if( $_FILES ) { var_dump($_FILES );} 

I am testing this code on local. Posting fields like string or integer is working fine but posting an image or any other file is not giving me any output. Am I missing some options in cURL or doing something wrong? I have checked the docs already php docs. Help me out please.

And please comment before downvoting :/ if you have to :(

2 Answers 2

1

$target must be "http://[HOST]/url/curlUploadHandler.php"

Check for errors

if( $errno = curl_errno($curl) ) { $error_message = curl_strerror($errno); echo "cURL error ({$errno}):\n {$error_message}"; } 

and

  • put in your code echo getcwd(); to check the full path
  • check with is_readable the image
  • check permisions of image
Sign up to request clarification or add additional context in comments.

9 Comments

I Know that. Everything is fine. I need to know how to post files through cURL. That url word is representing my whole url
what version of php do you have ?
I am using php 5.5.12 @CatalinB
image is in the same directory ? check for errors if($errno = curl_errno($curl)) { $error_message = curl_strerror($errno); echo "cURL error ({$errno}):\n {$error_message}"; }
1) put in your code echo getcwd(); to check the full path; 2) check with is_readable the image; 3) check permisions of image
|
1

Use :

$target="url/curlUploadHandler.php"; $file_name_with_full_path = realpath('down.png'); $post = array('extra_info' => 'test','file_contents'=>'@'.$file_name_with_full_path); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$target); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $result=curl_exec ($ch); curl_close ($ch); print_r($result); 

You can also refer for better understanding : http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

7 Comments

using @ filename is deprecated. I am using php 5.5.12
@Dherya: wait let i check it first. just because i use old version of PHP.
Yes u r right. in PHP 5.5+ it is depricated : but now we should use wiki.php.net/rfc/curl-file-upload but if you still want to use this deprecated approach then you need to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
no i don't want to use deprecated approach @Jignesh M. Mehta
and still setting curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); is giving me error when i used your approach
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.