Today originally wanted to try a picture Cloud API, so local did a upload picture of the test, the results of the gray often depressed found before the good curl upload pictures actually dead or alive does not work, originally a few minutes to fix things, the results toss half of the genius finally found the reason, incredibly is a compatibility problem, really no language.
Web search PHP uploading images via curl is almost always similar to the following code:
Previously uploaded images are added an @ symbol in front of the picture path, such as:
$file = __DIR__ .‘/0634134726bc5b8b.jpg‘;$data = array(‘mypic‘=>‘@‘. $file);
This is OK, but now this is generally only applicable to versions below PHP5.6.
To 5.6来 said, directly add @ is wood useful.
There are two solutions: one is compatibility, and the other is the use of new methods.
1. Compatibility method:
A configuration parameter used primarily for CURL curlopt_safe_upload
Curlopt_safe_upload the default value in PHP5.5 is False
In PHP5.6, the default is true.
So just add a line to the force set to false on the line, as follows:
Note: The setting order of this parameter must be valid before setting the Curlopt_postfields parameter!!!
<?php$url =' http://127.0.0.1/test3.php ';$file =__dir__.'/0634134726bc5b8b.jpg ';$data =array ( ' mypic ' => ' @ '. $file); $curl = Curl_init (); curl_setopt ( $curl, Curlopt_url, $url) curl_setopt ( $curl, Curlopt_returntransfer, true", curl_setopt ( $curl, curlopt_post, true ); curl_setopt ( $curl, curlopt_safe_upload, false); curl_ Setopt ( $curl, Curlopt_postfields, $data); $content = curl_exec ( $curl); Curl_close ($ Curl);p Rint_r ( $content);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2. New ways to upload images:
Use the Curlfile class to process the file as follows:
$url =' http://127.0.0.1/test3.php ';$file =__dir__. '/0634134726bc5b8b.jpg '; $data = array ( ' mypic ' =>new curlfile ( $file)); $curl = Curl_init (); curl_setopt ( $curl, Curlopt_url, $url) curl_setopt ( $curl, Curlopt_returntransfer, true", curl_setopt ( $curl, curlopt_post, true ); curl_setopt ( $curl, Curlopt_postfields, $data); $content = curl_exec ( $curl); Curl_close ( $curl);p Rint_r ( $content);
I'm just the simplest application here, so refer to the official documentation below:
http://php.net/manual/en/class.curlfile.php
This has finally uploaded successfully ... Too pit Daddy, day time again was tossing and finishing.
PHP5.6 to upload images via Curl @ Invalid compatibility issue