Asked  7 Months ago    Answers:  5   Viewed   40 times

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.

 Answers

19

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);

Wednesday, March 31, 2021
 
ClmentM
answered 7 Months ago
12

Unfortunately, we don't have a specific example of YouTube API v3 uploads from PHP available yet, but my general advice is:

  • Use the PHP client library instead of cURL.
  • Base your code on this example written for the Drive API. Because the YouTube API v3 shares a common API infrastructure with other Google APIs, examples for doing things like uploading files should be very similar across different services.
  • Take a look at the Python example for the specific metadata that needs to be set in a YouTube v3 upload.

In general, there are a lot of things incorrect with your cURL code, and I can't walk through all the steps it would take to fix it, as I think using the PHP client library is a much better option. If you are convinced you want to use cURL then I'll defer to someone else to provide specific guidance.

Wednesday, March 31, 2021
 
Extrakun
answered 7 Months ago
48

The $_FILES array being populated on PUT requests. That's because a PUT request will specify the file name in the url and the file content in the body. Nothing more.

You'll have to use php://input as you already suggested.

upload.php

<?php
/* PUT data goes to php://input */
echo file_get_contents("php://input");

Then use the following curl command line:

curl --upload -H "X-TOKEN: test123" a.txt  http://localhost/upload.php

Update after comments: Using the --upload option should fit your needs. In difference to -X PUT together with -F, the data will be send raw and get not multipart/form-data encoded.. One of the hidden treasures of curl ;)

Friday, May 28, 2021
 
steros
answered 5 Months ago
94

Actually, HTML5 and the new File API does support uploading via XmlHttpRequest. It works beautifully in Firefox 4 and Chrome.

Wednesday, September 1, 2021
 
laurent
answered 2 Months ago
79

You can use the tFileFetch component that allows you to download files, however in its advanced settings you can enable the "upload files" which then uploads the files. As a bonus it does it in a multi-part http post.

See more: https://help.talend.com/display/TalendComponentsReferenceGuide61EN/tFileFetch

Thursday, October 21, 2021
 
akes406
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :