Send file via cURL from form POST in PHP
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@email.org: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);
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);
Php Curl send File with data
Here the sample curl request
$curlFile = curl_file_create($uploaded_file_name_with_full_path);
$post = array('val1' => 'value','val2' => 'value','file_contents'=> $curlFile );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$your_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
Don't forget to put the appropriate header.
You can also find good source here
Curl File Upload
to send file via CURL. Make sure that you are passing your file on file_contents
key in above code.
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));
?>
How can I use cURL to post form data in php?
since no answer got it right thus far (at least not with an approach that would work in php 5.6+), here goes: the equivalent php curl_ code would be:
$ch = curl_init ( 'https://apiprovider.com/api/v0/imports?token=[KEY]' );
curl_setopt_array ( $ch, array (
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array (
'file' => new CURLFile ( '/home/USERNAME/import.csv' )
)
) );
curl_exec ( $ch );
(i would also recommend setting CURLOPT_ENCODING to emptystring, especially if you expect the response to be compressible, that would be the equivalent of adding --compressed
to the curl command line, and might speed things up)
how to send a Data Form including file, from PHP ContactForm7 with Curl
I ended up doing this in functions.php
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
$files = $submission->uploaded_files();
$file_invoice = $files['file_invoice'][0];
$cFile = "";
if (function_exists('curl_file_create'))
{ // php 5.5+
$cFile = curl_file_create($file_invoice);
}
else { $cFile = '@' . realpath($file_invoice); }
$fields = array(
'user_mail' => $user_mail,
'user_phone' => $user_phone,
'file' => $cFile
);
$curl = curl_init();
$curlParams = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://....',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $fields
);
curl_setopt_array($curl, $curlParams);
$resp = curl_exec($curl);
$responseStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
Can't send file as form data using cURL
If you want to be able to access the file's contents using $app->request->post('content');
, your curl request must use a <
instead of an @
for the content
field,
curl -i http://localhost/test/index.php -X POST -F "name=file.png" \
-F "content=</var/www/html/test/file.png"
From curl's manpage:
To force the
'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload,
while the < makes a text field and just get the contents for that text field from a file.
By using @
, you marked the field as a file. Technically, this adds a Content-Disposition
header to the field (RFC 2388, if you're interested). PHP detects that and automatically stores the field inside $_FILES
instead of $_POST
, as Raphaël Malié remarked in a comment, which is why you can't access the contents using $app->request->post
.
You should consider to switch to using $_FILES
in your backend, though, if you want to support uploading using browsers' <input type=file>
elements and forms. Those always set a Content-Disposition
header.
Send file using multipart/form-data request in php
If you work with CURL, you have to just:
1, set header 'Content-Type' as 'multipart/form-data;'
2, set option 'RETURNTRANSFER' of curl to true (use option method of curl)
3, set option 'POST' of curl to true (use option method of curl)
4, get source of your file (what you get from fopen in PHP):
$tempFile = tempnam(sys_get_temp_dir(), 'File_');
file_put_contents($tempFile, $source);
$post = array(
"uploadedFile" => "@" . $tempFile, //"@".$tempFile.";type=image/jpeg",
);
5, use post method of CURL with parameter in $post variable
Related Topics
Laravel 5 - How to Access Image Uploaded in Storage Within View
Undefined Index: User_Id. Where, User_Id Is Session Variable
How to Store .Pdf Files into MySQL as Blobs Using PHP
Clear the Form Field After Successful Submission of PHP Form
Laravel 5.6 Class App\Http\Controllers\Postcontroller Does Not Exist
Can You Assign Multiple Variables At Once in PHP Like You Can With Java
How to Get Filename from a Variable (Url) in PHP
Display Data from Database to Dropdown Codeigniter
Convert Accented Characters to Their Plain Ascii Equivalents
How to Add Class to Link in Wp_Nav_Menu
Laravel 5.5 Http Error 500 Unable to Handle This Request
Object of Class Mysqli_Result Could Not Be Converted to String
Redirect That Same Page from Where Id Is Coming
Trim Country Code from Phone Number in PHP
Saving Multiple Records in a Laravel Eloquent Create
Warning: MySQL_Fetch_Array(): Supplied Argument Is Not a Valid MySQL Result
Laravel How to Return Single Column Value Using Query Builder