0

我目前正在使用此代码:

    <form enctype="multipart/form-data" method="post" action="upload_img.php">
    Choose your file here:
    <input name="uploaded_file" type="file"/>
    <input type="submit" value="Upload It"/>
    </form>
    <?
    if( isset($_FILES['uploaded_file']) )
{
    $IMGUR_API_KEY = 'APIKEY';
    $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);
    }
?>

效果很好,但是我需要将其用作单独的表单来插入多个字段。最后,我需要做的就是发出 AJAX cURL 请求来上传照片,然后将 imgur.com url 获取到直接图像。我将如何使此表单成为通过 AJAX 执行此操作的提交,以便我可以留在此页面上...任何帮助将不胜感激,它不必完全编写,而是一个想法的火花。到目前为止,我不知道我要去哪里。

谢谢, HackyWackee

4

1 回答 1

0

您可以尝试类似或类似的方法:

    <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" SRC="upload.php" width="100%" height="200px" id="iframe1" marginheight="0" frameborder="0" onLoad="autoResize('iframe1');"></iframe>

然后将您的 php 放入“upload.php”。

于 2013-01-29T02:05:28.817 回答