How to upload a file using curl with php

I want to know how to upload a file using cURL or anything else in PHP. I searched google many times, but there were no results.

I have this code to get the file and upload it

the code:

echo"".$_FILES['userfile'].""; $uploaddir = './'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if ( isset($_FILES["userfile"]) ) { echo '<p><font color="#00FF00" size="7">Uploaded</font></p>'; if (move_uploaded_file ($_FILES["userfile"]["tmp_name"], $uploadfile)) echo $uploadfile; else echo '<p><font color="#FF0000" size="7">Failed</font></p>'; } 

I want the code to send the file to the recipient file.

+44
php upload curl
Mar 04
source share
1 answer

Using:

 if (function_exists('curl_file_create')) { // php 5.5+ $cFile = curl_file_create($file_name_with_full_path); } else { // $cFile = '@' . realpath($file_name_with_full_path); } $post = array('extra_info' => '123456','file_contents'=> $cFile); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$target_url); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $result=curl_exec ($ch); curl_close ($ch); 

You can also contact:

http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

Important tip for PHP 5.5+:

Now we should use https://wiki.php.net/rfc/curl-file-upload , but if you still want to use this deprecated approach, you need to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

+91
Mar 04 '13 at 11:46 on
source share



All Articles