PHP Curl Upload Images

Asked

Viewed 834 times

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 -->

Manually making (accessing the site from my browser) and collecting the array sent by post, I have basically this (tested by sending 1 photo any 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 image is 377529090726537.jpg does not come from my PC. The image in question had another name. Soon the form through the POST already treated the image that was uploaded. Probably this name ai is already return of the script.

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

The question is: Unlike the upload that is done via form to which the person selects the image path and sends by the form that treats the image, this system uploads the image in the first part of the form where you fill other fields.

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

In order to learn something, suppose 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 proceed to upload to an external system through the cURL?

1 answer

2

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

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

Here’s an example of code 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);
  • Can you explain the attributes of the? tmp_name, type and name command?

  • is the values that come in the variable $_FILES, as I pass it by function, I end up rescuing in the variable $data

  • Considering my foreach with this structure 'foreach ($xml->file[$i]->photos->photo as $photo) {' how I could use this curl_file_create?

  • in the foreach your variable $foto contains the values of tmp_name, type and name? If yes, just put $post[$key] = curl_file_create($foto['tmp_name'], $foto['type'], $foto['name']);

  • No... in my foreach it returns the full URL of the image, example: 'http://www.site.com/camio/das/imagens/padrao/imagem12031931039.jpeg' Just that. That’s why it’s hard to understand your idea.

  • My code uploads directly to cURL without the need to upload to your server, it works with the temporary file and not with the actual file, to get straight from a url would be different

  • Gee, complicated! hahhaaha. And if I use regex to extract the path from the url and add the path to the server, how would it look? Using the link example above, use a regex to get: /usr/www/meusite.com/camunho/das/imagens/padrao/imagem12031931039.jpeg and use later curl_file_create($caminhoabsolutoimagem)'being that $caminhoabsolutoimagem regex result of $foto ?

  • 1

    I am not good at regex, I will try to create the script to send the physical file, anything edits my answer

  • I think you don’t even need regex, I did so and got the absolute path through the received url in $foto of my foreach: $foto = parse_url($foto);&#xA; $caminhoabsoluto = $foto['path'];&#xA; echo ($_SERVER['DOCUMENT_ROOT'].$caminhoabsoluto);

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.