0

I am trying to integrate imgur to my website. I currently have the HTML and php code. It works fine. The only thing is that it doesn't display the uploaded image in the current page, but it opens a new page. Is there a way to load the image in the current page?

Here's the code:

<form action="upload.php" enctype="multipart/form-data" method="POST">
 Choose Image : <input name="img" size="35" type="file"/><br/>
 <input type="submit" name="submit" value="Upload"/>
</form>

The php file:

<?
$img=$_FILES['img'];
if(isset($_POST['submit'])){ 
    if($img['name']==''){  
        echo "<h2>An Image Please.</h2>";
    }else{
        $filename = $img['tmp_name'];
        $client_id="my-id";
        $handle = fopen($filename, "r");
        $data = fread($handle, filesize($filename));
        $pvars   = array('image' => base64_encode($data));
        $timeout = 30;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
        $out = curl_exec($curl);
        curl_close ($curl);
        $pms = json_decode($out,true);
        $url=$pms['data']['link'];
        if($url!=""){
            echo "<h2>Uploaded Without Any Problem</h2>";
            echo "<img src='$url'/>";
        }else{
            echo "<h2>There's a Problem</h2>";
            echo $pms['data']['error'];  
        } 
    }
}
?>
Horay
  • 1,388
  • 2
  • 19
  • 36

1 Answers1

0

change the form action

action="upload.php"

to self

also your php should start with

<?php 
Lucky Chingi
  • 2,248
  • 1
  • 10
  • 15
  • The php is in a different file. I just updated the question saying that – Horay Nov 10 '15 at 03:28
  • I get the following error when I do that: Not Found The requested URL /self was not found on this server. Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80 – Horay Nov 10 '15 at 16:43
  • Oh. Now it works. But is there a way to do it without reloading the page? – Horay Nov 10 '15 at 20:58
  • The form submit will always load the page, dig into Ajax if you dont want the page to load. – Lucky Chingi Nov 10 '15 at 21:03
  • Your ajax can send the request to a PHP page and that page can process the way you want. If this question has been answered, mark it as closed and try AJAX before posting a question about ajax image upload - dont want down votes ;) – Lucky Chingi Nov 10 '15 at 21:18
  • Thanks! Here's a new question if you don't mind answering. Really appreciate it! http://stackoverflow.com/questions/33640571/upload-image-to-imgur-using-ajax – Horay Nov 10 '15 at 22:15