How to Upload File Using Curl With PHP

how to upload file using curl with PHP

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

How to POST upload a file via PHP CURL

You should do something like this

$postData = array(
'folderId' => '3276800',
'filename' => 'TxT_12323.txt',
'filedata' => file_get_contents('847327489732984.txt'),
);

For the most part you can just treat any file as string data in PHP. That doesn't mean you can read and make sense of them, but they can be represented that way in a variable or output and should work just fine.

PHP upload file with curl

Here is the answer to your question (as it's repeated question!):

how to upload file using curl with php

And also take a look at the link here:
http://php.net/manual/en/function.curl-file-create.php

File uploading through curl in php

What version of PHP are you using? In PHP 5.5 the curl option CURLOPT_SAFE_UPLOAD was introduced which startet defaulting to true as of PHP 5.6.0. When it is true file uploads using @/path/to/file are disabled. So, if you are using PHP 5.6 or newer you have to set it to false to allow the upload:

curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);

But the @/path/to/file format for uploads is outdated and deprecated as of PHP 5.5.0, you should use the CurlFile class for this now:

$request = curl_init();
$file_path = $path.$name;
curl_setopt($request, CURLOPT_URL, 'http://localhost/pushUploadedFile.php');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => new CurlFile( $file_path ),
'test' => 'rahul'
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

How to upload file by PHP cURL using ftp connection?

To upload the file in curl you can use the curl_file_create.Try the below one:

$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$curl_file = curl_file_create($localfile,'zip');
$params = ['file' => $curl_file];

$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass@$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);

Use CURL to send a file and parameters in PHP

Uploading files with cURL will become available like just a regular HTTP FILE POST and should be available through $_FILE global variable to be handled the same regular way you'd handle a regular file upload with PHP.

test.php

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, "http://localhost/Projects/Test/test-response.php");
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

curl_setopt($cURL, CURLOPT_POSTFIELDS, [
"ID" => "007",
"Name" => "James Bond",
"Picture" => curl_file_create(__DIR__ . "/test.png"),
"Thumbnail" => curl_file_create(__DIR__ . "/thumbnail.png"),
]);

$Response = curl_exec($cURL);
$HTTPStatus = curl_getinfo($cURL, CURLINFO_HTTP_CODE);

curl_close ($cURL);

print "HTTP status: {$HTTPStatus}\n\n{$Response}";

test-response.php

print "\n\nPOST";
foreach($_POST as $Key => $Value)print "\n\t{$Key} = '{$Value}';";

print "\n\nFILES";
foreach($_FILES as $Key => $Value)print "\n\t{$Key} = '{$Value["name"]}'; Type = '{$Value["type"]}'; Temporary name = '{$Value["tmp_name"]}'";

Output

HTTP status: 200

POST
ID = '007';
Name = 'James Bond';

FILES
Picture = 'test.png'; Type = 'application/octet-stream'; Temporary name = 'C:\Windows\Temp\php76B5.tmp'
Thumbnail = 'thumbnail.png'; Type = 'application/octet-stream'; Temporary name = 'C:\Windows\Temp\php76C6.tmp'

I assume you will keep the relevant 2 images in the same path as 'test.php' to obtain the output as shown.

How to upload files with POST form-data in PHP?

Try this one:

<?php

$url = "https://sm.ms/api/v2/upload";

// I guess the file is in the same directory as this script
$file = __DIR__.'/test.png';

$headers = [
'Content-Type: multipart/form-data',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT'],
];

$fields = [
'smfile' => new CURLFile($file, 'image/png')
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

var_dump(curl_exec($ch));
var_dump(curl_error($ch));

?>

Upload a Pdf file to another website using cURL

Well after a lot of trying out different methods I passed this value to the post field directly without using it as array

CURLOPT_POSTFIELDS => “foo=$mj&callbackurl=url”,

and it worked. This was where I was stucked. I did it the other way and it didn't work. Each situation is unique. Someone could also need this.



Related Topics



Leave a reply



Submit