1

I'm trying to upload an image to IMGUR via PHP. This is the code:

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

// $data is file data
$pvars   = array('image' => base64_encode($data), 'mykey' => 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);

?>

This is the error message I receive:

Warning: fopen(image.jpg) failed to open stream: No such file or directory

I don't understand the part: $filename = "image.jpg"; Where does the filename come from since it's a base64 generated string? Thanks, Bob

Michael
  • 327
  • 2
  • 6
  • 12
  • It means PHP can't find 'image.jpg' and so failed to "open" it. If the image file isn't in the same directory as this script, you'll have to specify a path pointing to the image. Either an absolute path (`/path/to/image`) or a path relative to the script (`../../elsewhere/image.jpg`). – Marc B Apr 12 '11 at 19:38

2 Answers2

2

That warning is because fopen is trying to read in the file image.jpg from the directory from which your script is running. A good example on how to transfer a file through curl can be seen here

Send file via cURL from form POST in PHP

Where they have $localFile = $_FILES[$fileKey]['tmp_name']; you would put $localFile = '/path/to/image.jpg'; As well as change the server info and add in any other variables you may need to pass to imgur.

Community
  • 1
  • 1
Ryan Matthews
  • 1,043
  • 9
  • 28
  • I don't undestand where "image.jpg" comes from. In my understanding the PHP generates it from the base64 string I send and then put it in the folder where the PHP file is in, right? – Michael Apr 12 '11 at 19:58
  • OK I think I found it. Just needed to delete this part: `$filename = "image.jpg"; $handle = fopen($filename, "r"); $data = fread($handle, filesize($filename));` But there is still a question open - how do I receive a callback or response from IMGUR with the path to the uploaded image? – Michael Apr 12 '11 at 22:21
  • You will get back an xml response. You need to pull out the URL tag from the response that is save in the variable $xml. The documentation is here: http://api.imgur.com/responses – Ryan Matthews Apr 13 '11 at 13:39
  • I'm using Actionscript 3 (AS3) with the PHP to upload the image. I took this from the IMGUR examples but it returns just empty: `var iresponse:XML = new XML(unescape(loader.data));` Any ideas? – Michael Apr 13 '11 at 15:42
  • How can I send the response from PHP to Flash? – Michael Apr 14 '11 at 15:14
  • all you should have to do is echo out the xml and flash should get it. I dont know too much about flash though. If everything went well you should be able to test just the php script and echo out the xml in a browser to see the source code of the return. – Ryan Matthews Apr 14 '11 at 16:53
  • It's working, thanks. But it's return an error that the image is corrupt. I'm passing from FLASH a base64 data string like said in the documentation. Is this PHP code right for that? http://pastebin.com/r6ZJcBn8 – Michael Apr 14 '11 at 18:50
  • The api says you can upload a binary file as well, you may want to try that solution. This is the url I am getting my info from http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html your $post_array var though would include image=> and key=> – Ryan Matthews Apr 14 '11 at 19:15
  • Doesn't help. I send a base64 encoded data to the PHP - so do I need to remove the "base64_encode" part from inside the PHP cause it's already encoded? here is the code: + http://pastebin.com/r6ZJcBn8 – Michael Apr 14 '11 at 22:45
  • If you are sending it the image data already base64_encoded you would have to remove that part because it is already encoded. – Ryan Matthews Apr 15 '11 at 12:36
0

Change line 1 from:

$filename = "image.jpg";

To:

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

Then, to post... I recommend a form similar to this:

    <form enctype="multipart/form-data" method="post" action="upload.php" target="my_iframe">
    Choose your file here:
    <input name="uploaded_file" type="file"/>
    <input type="submit" value="Upload It"/>
    </form>
<!-- when the form is submitted, the server response will appear in this iframe -->
<script language="JavaScript">
<!--
function autoResize(id){
    var newheight;
    var newwidth;

    if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
        newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
    }

    document.getElementById(id).height= (newheight) + "px";
    document.getElementById(id).width= (newwidth) + "px";
}
//-->
</script>

<IFRAME name="my_iframe" width="100%" height="200px" id="iframe1" marginheight="0" frameborder="0" onLoad="autoResize('iframe1');"></iframe>

If you put all your php into upload.php and then have that form on a page in the same directory, it's pretty close to being functional... Except you don't yet have an API_KEY in your source. You can get an API KEY here: https://imgur.com/register/api_anon

In the end your php should look like this:

    <?
    if( isset($_FILES['uploaded_file']) )
{
    $IMGUR_API_KEY = 'u432ewriuq3oirefuie'; //put your api key here
    $filename = $_FILES['uploaded_file']['tmp_name'];
    $handle = fopen($filename, "r");
    $data = fread($handle, filesize($filename));

    //$data is file data
    $pvars   = array('image' => base64_encode($data), 'key' => $IMGUR_API_KEY);
    #$pvars   = array('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_URL, 'http://api.imgur.com/2/gallery.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);
    $xmlsimple = new SimpleXMLElement($xml);
    echo '<img height="180" src="';
    echo $xmlsimple->links->original;
    echo '">';

    curl_close ($curl);
    }
?>
grepsedawk
  • 3,324
  • 2
  • 26
  • 49