Viewed   79 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

3

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

Monday, September 12, 2022
2

Try adding processData: false, contentType: false in your code

Replace your script with this:

function submitDocument(){
var formData = new FormData(); // Currently empty
var _token = $("#_token").val().trim();
formData.append('title', $("#title").val());
formData.append("doc",$("#doc")[0].files[0]);
$.ajax({
  url: "documents",
  method: "post",
  data:{_token,formData},
  cache : false,
  processData: false,
  contentType: false
}).done(function(data) {

});
return false;// Not to submit page
}

By default, data passed in to the data option as an object will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

Tuesday, November 29, 2022
 
3

File upload using curl does not work like this. You need to first save the file locally using php's move_uploaded_file then get the path to file. In the fields add this,

$fields = array(
    'file'=>"@/path/to/myfile.ext",
    'username'=>urlencode($_POST["username"]),
    'password'=>urlencode($_POST["password"]),
    'latitude'=>urlencode($_POST["latitude"]),
    'longitude'=>urlencode($_POST["longitude"]),
);

Also, I'm not sure if fields array needs to be converted to string to be used as postfields. According to manual it can be an array() directly.

CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work.

Friday, September 9, 2022
3

You want to put the file in the body (i.e. in the CURLOPT_POSTFIELDS). You'll want to specify Content-Type matching the file type and Content-Length as well.

This is well documented.

File metadata should be going to /drive/v2/files, not /upload/drive/v2/files.

This way, you'd have to make two requests.

If you want to do both a file and meta simultaneously, you can use the API and file insert:

https://developers.google.com/drive/v2/reference/files/insert

Note that there's a PHP library to make this easier.

Wednesday, September 14, 2022
 
quarra
 
3

Firstly, I notice that your click event for #upload_image fires a click trigger on #bundle_user_file, but below that you are asking it to look for a change event. Therefore, this would do nothing.

You can re-generate a CSRF token if you want by calling the csrf token_manager service by doing this:

/** @var SymfonyComponentSecurityCsrfCsrfTokenManagerInterface $csrf */
$csrf = $this->get('security.csrf.token_manager');
$token = $csrf->refreshToken($tokenId);

return new Response($token);

You can determine $tokenId in your form, if you want, or just use your picture ID, or whatever. Normally the CSRF token is generated automatically from your session, so you might want to check that too.

Monday, September 19, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :