PHP Curl Image Upload

0

I'm trying to upload using cURL in PHP on an external page. At first the command line of the external page that captures the photos is exactly this:

<!-- BEGIN FIELD IMAGES -->

<div class="form-col col-1">
    <label class="form-label label-with-obs">
        Adicionar imagens
    </label>
    <span class="form-label-obs">Até 6 imagens, extensões permitidas JPG, GIF e PNG somente.</span>
</div>
<div class="form-col col-2">
    <div class="line">
        <ul id="uploaded_images" class="list_ai-images"></ul>
        <div class="main-image-box hidden">
            <span class="main-box-label">Principal</span>
            <div class="image_box"></div>
        </div>
        <div id="wrapper_image_upload_button" class="wrapper-upload-button add-image">
            <div class="image">
                <span id="image_upload_button" class="btn-upload-image"></span>
            </div>
        </div>
    </div>
    <p id="err_extra_image" class="form-validation form-validation-success" style="display:none;">

</p>

    <p id="err_image" class="form-validation form-validation-success" style="display:none;">

</p>

    <p id="image_upload_status" class="mage_upload_alert form-validation form-validation-error"></p>
</div>
<!-- END FIELD IMAGES -->

By manually (accessing the site from my browser) and collecting the array sent by the post, I have basically this (I tested sending any photo from my PC):

Array
(
    [image] => Array
        (
            [0] => 377529090726537.jpg
        )

    [thumbnail_digest] => Array
        (
            [0] => EMPTYDIGEST
        )

    [digest_present] => Array
        (
            [0] => 0
        )

    [image_rotation] => Array
        (
            [0] => 0
        )
)

PS: This 377529090726537.jpg image is not coming from my PC. The image in question had a different name. Soon the form through POST already treated the image that was uploaded. Probably this name is already a return of the script.

That is, it is still uploaded on the form page and when it is sent to "handling" via Post, the name of the image that was hosted on the server is already returned.

Doubt is: Unlike sending via a form to which the person selects the path of the image and sends it through the form that treats the image, this system uploads the image still on the first part of the form where you fill in other fields.

In this situation, how could I succeed in uploading an image (or rather 6 images) through the cURL function of php? All the rest of the form I can fill, I can send title, message body, select input and combobox ... but this one of the upload is new for me.

In order to do something didactic, let's say that the image file I want to upload is called: IMAGEM1.JPEG and IMAGEM2.JPEG , they are available in the same folder as my curl script, how to upload to a external system through cURL ?

    
asked by anonymous 29.10.2015 / 13:01

1 answer

2

You can use curl_file_create() , you do not have to upload to the server to send by cURL , you can send direct only using this function.

$post[$key] = curl_file_create($file['tmp_name'], $file['type'], $file['name']);

Here is an example code that I use to upload via cURL :

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, ($url));
curl_setopt($ch, CURLOPT_POST, true);

$post = array();

unset($data['file']);
foreach ($data as $key => $value) {
    if(is_array($value)):
        if($value['error'] == 0):
            $post[$key] = curl_file_create($value['tmp_name'], $value['type'], $value['name']);
        endif;
    else:
        $post[$key] = $value;
    endif;       
}

curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

$response = curl_exec($ch);

curl_close($ch);
    
29.10.2015 / 13:12