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

1
  • 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). Commented Apr 12, 2011 at 19:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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?
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?
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: api.imgur.com/responses
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?
How can I send the response from PHP to Flash?
|
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);
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.