Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send file via cURL from form POST in PHP

I'm writing an API and I'm wanting to handle file uploads from a form POST. The markup for the form is nothing too complex:

<form action="" method="post" enctype="multipart/form-data">   <fieldset>     <input type="file" name="image" id="image" />     <input type="submit" name="upload" value="Upload" />   </fieldset> </form> 

However, I'm having difficulties understanding how to handle this server-side and send along with a cURL request.

I'm familiar with sending POST requests with cURL with a data array, and resources I've read on uploading files tell me to prefix the filename with an @ symbol. But these same resources have a hard-coded file name, e.g.

$post = array(     'image' => '@/path/to/myfile.jpg',     ... ); 

Well which file path is this? Where would I find it? Would it be something like $_FILES['image']['tmp_name'], in which case my $post array should look like this:

$post = array(     'image' => '@' . $_FILES['image']['tmp_name'],     ... ); 

Or am I going about this the wrong way? Any advice would be most appreciated.

EDIT: If someone could give me a code snippet of where I would go with the following code snippets then I'd be most grateful. I'm mainly after what I would send as cURL parameters, and a sample of how to use those parameters with the receiving script (let's call it curl_receiver.php for argument's sake).

I have this web form:

<form action="script.php" method="post" enctype="multipart/form-data">   <fieldset>     <input type="file" name="image />     <input type="submit" name="upload" value="Upload" />   </fieldset> </form> 

And this would be script.php:

if (isset($_POST['upload'])) {     // cURL call would go here     // my tmp. file would be $_FILES['image']['tmp_name'], and     // the filename would be $_FILES['image']['name'] } 
like image 224
Martin Bean Avatar asked Nov 19 '10 10:11

Martin Bean


People also ask

How to send a file via curl?

To post a file with Curl, use the -d or -F command-line options and start the data with the @ symbol followed by the file name. To send multiple files, repeat the -F option several times.

What is use of 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. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.


1 Answers

Here is some production code that sends the file to an ftp (may be a good solution for you):

// This is the entire file that was uploaded to a temp location. $localFile = $_FILES[$fileKey]['tmp_name'];   $fp = fopen($localFile, 'r');  // Connecting to website. $ch = curl_init();  curl_setopt($ch, CURLOPT_USERPWD, "[email protected]:password"); curl_setopt($ch, CURLOPT_URL, 'ftp://@ftp.website.net/audio/' . $strFileName); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_NOPROGRESS, false); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback'); curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile)); curl_exec ($ch);  if (curl_errno($ch)) {      $msg = curl_error($ch); } else {      $msg = 'File uploaded successfully.'; }  curl_close ($ch);  $return = array('msg' => $msg);  echo json_encode($return); 
like image 150
Jeff Davis Avatar answered Sep 21 '22 02:09

Jeff Davis