• 1

[PHP ]Uploading images from hard drive


Hi! i 'm trying to post an item with pictures. I'm able to do so if i use public images that are hosted in some web pages 

Is it possible to use images from your own hard drive? without uploading them to a server?

This is my code:

$params = array('access_token' => $_SESSION['access_token']);

$body = array("price" => '2',
              "title" => 'LOCAL 2ND TRIAL',
              "category_id"=> 'MLA40490',

$response = $meli->post('/items', $body, $params); 

It works perfectly if i replace the image url by a public one. The problem is i have hundreds of pics that i dont want to upload to a public server (it defeats the purpose of the app i'm writing).

Is it possible to upload images directly from the hard drive??

Thanks in advance!!







Edited by vittorio

Share this post

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

after struggling for many days I finally wrote a function that uploads a picture from the hard drive directly to the mercadolibre server, you can add it as a method to the meli class, even though it doesnt use the internal logic, i leave that as a TODO:

    public function upload_pic_from_harddrive($uploads_dir, $tmp_name, $name) {

        //$uploads_dir ----------> directory where the images are moved to after having  been uploaded using an html5 form e.g.: "C:/xampp/htdocs/images";

        //$tmp_name -----------> $_FILES['file']['tmp_name'];         -----------------------> REPLACE 'file' with the  value of the 'name' atribute in the html5 form

        //$name -------------------->  basename($_FILES["file"]["name"]); ---------------------------> REPLACE 'file' with the  value of the 'name' atribute in the html5 form

        $target_url = 'https://api.mercadolibre.com/pictures?access_token='. $_SESSION['access_token'];           
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
        $fname = "C:/xampp/htdocs/images/".$name;
        $cfile = new CURLFile(realpath($fname));
        $post = array (
                      'file' => $cfile

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target_url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);   
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);  
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        $result = curl_exec ($ch);

        if ($result === FALSE) {
            return "Error sending" . $fname .  " " . curl_error($ch);
            curl_close ($ch);
            curl_close ($ch);
            return  $result;



I hope it helps somebody with the same problem. 




Share this post

Link to post
Share on other sites
  • 0

Thanks for your answer juvian!

The problem is, I had already tried that, but the picture never shows. All i see is the "processing image"  default image.

The picture gets uploaded to the mercadolibre server (supposedly), since i get a 201 code when I examine the response. I also get the uploaded picture id and i'm able to retrieve the uploaded picture from the api. But there´s no way to see the picture. However if I use a picture already uploaded to certain public servers, the picture is visible inmeadiately. Last night I uploaded a picture directly from my hard drive using the code you mentioned and this morning still was "in process" so I take it that means it will never get uploaded. (I can´t wait 12 hours to upload a photo.)

Here´s my code:

//upload an image from the local hard drive to the mercadolibre server:


$response2 = $meli->post('/pictures', $body2, $params);

//associate the recently uploaded picture to a previously created post

$body3=array("id"=>"897268-MLA26367623449_112017", "max_size"=>"100*100");

$response3 = $meli->post('/items/MLA692059690/pictures', $body3, $params);

Please note the part in bold, i'm using a file path, not an url because i'm trying to upload the pictures directly from the hard drive.

Like i said i 'm able to do so, or at least both requests have a 201 response. But the picture never shows.

Juvian, could you walk me through  what I'm doing wrong? I need to upload the pictures directly from the hard drive.

A million thanks in advance!!










Share this post

Link to post
Share on other sites
  • 0

Well, clearly ml has no way to access your internal file image just from your computer path. You need to upload the image binary data, in a special format. There is not much documentation about it, you can either try the curl example they give in their docs and then set curl debugging options to check how is curl sending the data, or you can just search how file upload is done in general: https://stackoverflow.com/questions/21905942/posting-raw-image-data-as-multipart-form-data-in-curl.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now