PHP5.6 to upload images via Curl @ Invalid compatibility issue

Source: Internet
Author: User

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);
    • 1
    • 2

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);      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.