4

I came across http://api.imgur.com and thought that would be a usefull tool to use on my website. I then noticed that StackOwerflow uses it too, so it must be good ))) Although I'm struggling when I try to implement it. I took a look at http://api.imgur.com/examples the PHP section, but it didn't help me much.

What I'm interested in is includin imgur api on my website so users can upload their images. I would than need to store img url/path so I can display it on a website.

e.g. have a form that will allow users to upload photo, then store url/path of the uploaded image in a database (VARCHAR).

Has anyone had a success with this system and could help me understand how to implement it like StackOwerflow uses it (only store image url in database not post).

Code I tried:

 <form enctype="multipart/form-data" method="post" action="upload_img.php">
    Choose your file here:
    <input name="uploaded_file" type="file"/>
    <input type="submit" value="Upload It"/>
    </form>

upload_img.php

<?
    $filename = "image.jpg";
    $handle = fopen($filename, "r");
    $data = fread($handle, filesize($filename));

    // $data is file data
    $pvars   = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
    $timeout = 30;
    $curl    = curl_init();

    curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);

    $xml = curl_exec($curl);

    curl_close ($curl);
?>
Ilja
  • 44,142
  • 92
  • 275
  • 498
  • 3
    What, specifically, are you having difficulty with? – Michael Petrotta Dec 01 '11 at 18:18
  • Well, implementing it on a website, in a sort of form. Say user clicks on "choose File" button and select's image. How do I make it so it uploads to imgur.com ? – Ilja Dec 01 '11 at 18:20
  • Right, I get that. What part of the documentation are you having issues with? We like specific questions here. – Michael Petrotta Dec 01 '11 at 18:21
  • I mentioned it in the post http://api.imgur.com/examples Im interested in PHP version, but I don't get the idea about how it works. – Ilja Dec 01 '11 at 18:22
  • 3
    One more try. What *specific* part of the information at that link is tripping you up? What did you try, and where are you stuck? – Michael Petrotta Dec 01 '11 at 18:24
  • What I tried is, when user submits a form (with a chosen file) I lunch the PHP from the link (with my details added), but no image gets uploaded to my imgur.com account. – Ilja Dec 01 '11 at 18:26
  • Sorry for not being specific, English is not my first language. – Ilja Dec 01 '11 at 18:27
  • 4
    Your English is fine. Your detail is lacking. Post what you've tried (code!) and describe exactly how it's not performing well. – Michael Petrotta Dec 01 '11 at 18:29

1 Answers1

4

I see that you have copy and pasted the example from the imgur API site, which gives the example of a filename which is contained within $filename. You need to point this variable at the file that PHP has uploaded.

Modify the $filename part of your script:

$filename = $_FILES['uploaded_file']['tmp_name'];

Source: POST method file uploading

Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72