Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload file using curl with PHP [closed]

Tags:

php

curl

upload

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

In other words, the user sees a file upload button on a form, the form gets posted to my php script, then my php script needs to re-post it to another script (eg on another server).

I have this code to receive the file and upload it

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 receiver file.

like image 365
Hadidi44 Avatar asked Mar 04 '13 11:03

Hadidi44


People also ask

How do I upload a file using cURL?

How to send a file using Curl? To upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server. Curl will use the file extension to send the correct MIME data type.

Can I use cURL in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.

Can cURL transfer files?

Uploading files using CURL is pretty straightforward once you've installed it. Several protocols allow CURL file upload including: FILE, FTP, FTPS, HTTP, HTTPS, IMAP, IMAPS, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, and TFTP. Each of these protocols works with CURL differently for uploading data.


1 Answers

Use:

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 refer:

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

Important hint 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 then you need to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

like image 167
karthik Avatar answered Sep 26 '22 08:09

karthik