在当前页面中加载imgur图像

时间:2015-11-10 03:16:44

标签: php html ajax imgur

我正在尝试将imgur集成到我的网站。我目前有HTML和PHP代码。它工作正常。唯一的事情是它不会在当前页面中显示上传的图像,但会打开一个新页面。有没有办法在当前页面中加载图像?

以下是代码:

<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>

php文件:

<?
$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'];  
        } 
    }
}
?>

1 个答案:

答案 0 :(得分:0)

更改表单操作

action="upload.php"

自我

你的php应该以

开头
<?php