cURL file uploads not working anymore after upgrade from PHP 5.5 to 5.6
Actually I found the answer while starting the question. There is a new Variable included with curl in PHP 5.5: CURLOPT_SAFE_UPLOAD
this is set to false
by default in PHP 5.5 and is switched to a default of true
in PHP 5.6.
This will prevent the '@' upload modifier from working for security reasons - user input could contain malicious upload requests. You can use the CURLFile
class to upload files while CURLOPT_SAFE_UPLOAD
is set to true
or (if you're sure your variables are safe you can switch the CURLOPT_SAFE_UPLOAD
to false
manually):
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
Here's a source for the information that got me searching in the right direction: http://comments.gmane.org/gmane.comp.php.devel/87521
It's mentioned in the changed functions too: http://php.net/manual/en/migration56.changed-functions.php
But not in the backward incompatible changes, really tripped me off...
PHP 5.6 / cURL post file
Try adding this to your code: curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
Assuming that your insert.php
is in the same server you are trying to upload the file to, this should work. Else, for PHP v5.6 you may try curl_file_create()
. A nice tutorial is here which checks for curl_file_create()
and if not, falls back to the older way of uploading file.
See Also: cURL file uploads not working anymore after upgrade from PHP 5.5 to 5.6
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);
PHP - cURL file from localhost not working
check you php.ini, your apache conf files, restart, and repeat and repeat and repeat...
or if you're having the same problem as me, and know that curl is loaded, but just not executing external requests, try adding this option to your curl
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
This option determines whether curl verifies the authenticity of the
peer's certificate
src
The problem is likely caused by having an out-to-date certificate.
Especially if you're developing on Windows and use XAMPP or some comparable service, the certificates are not loaded by default. On Linux this is less likely to be required.
For production use, you should fix the root problem instead of allowing this vulnerability to affect your server communication.
Curl Not working to upload a file on server
You could try...
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
Depending on PHP version, this is defaulted to false
in php 5.5 and true
in 5.6+ ... so check local and webserver version of PHP.
See: http://php.net/manual/en/function.curl-setopt.php
Can anyone give me an example for PHP's CURLFile class?
There is a snippet on the RFC for the code: https://wiki.php.net/rfc/curl-file-upload
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile('filename.png', 'image/png', 'filename.png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
You can also use the seemingly pointless function curl_file_create( string $filename [, string $mimetype [, string $postname ]] )
if you have a phobia of creating objects.
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = curl_file_create('filename.png', 'image/png', 'filename.png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
Related Topics
But These Conflict with Your Requirements or Minimum-Stability
PHP Switch Case More Than One Value in the Case
How to Strip Out the Domain Name from a Url in PHP
How to Capture Search Term from Google Search
How to Remove Null Values from an Array
How to Convert Between Time Zones in PHP Using the Datetime Class
Call to Undefined Function Curl_Init() Even It Is Enabled in PHP7
Utf-8 in PHP Regular Expressions
Using Print_R and Var_Dump with Circular Reference
Accented Characters in MySQL Table
Fatal Error: Call to Undefined Function Pg_Connect()
How to Connect to Database on Another Server