Upload image to imgur using ajax

后端 未结 1 1262
失恋的感觉
失恋的感觉 2020-12-22 11:11

I\'m trying to load an image from imgur on the same page, without reloading. I used ajax to perform that.

I tried doing it without ajax, an

相关标签:
1条回答
  • 2020-12-22 11:41
    <?php
    if(!@$_SERVER['HTTP_REFERER']) die('No direct Access');
    $img=$_FILES['img'];
        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'];  
            } 
        }
    ?>
    

    you dont need

    if(isset($_POST['submit']))
    

    in upload.php

    0 讨论(0)
提交回复
热议问题