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
?
Can you explain the attributes of the? tmp_name, type and name command?
– Diego
is the values that come in the variable
$_FILES
, as I pass it by function, I end up rescuing in the variable$data
– Jeferson Assis
Considering my foreach with this structure 'foreach ($xml->file[$i]->photos->photo as $photo) {' how I could use this curl_file_create?
– Diego
in the
foreach
your variable$foto
contains the values oftmp_name
,type
andname
? If yes, just put$post[$key] = curl_file_create($foto['tmp_name'], $foto['type'], $foto['name']);
– Jeferson Assis
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.
– Diego
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– Jeferson Assis
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 latercurl_file_create($caminhoabsolutoimagem)'
being that$caminhoabsolutoimagem
regex result of$foto
?– Diego
I am not good at regex, I will try to create the script to send the physical file, anything edits my answer
– Jeferson Assis
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);
 $caminhoabsoluto = $foto['path'];
 echo ($_SERVER['DOCUMENT_ROOT'].$caminhoabsoluto);
– Diego