2011-11-01 21 views
0

我使用表單上傳壁紙,我們手動選擇一個圖像文件..我想從一個圖像URL(圖像在互聯網..例如www.mysite.com/images/my。 JPG),這意味着沒有選擇一個圖像文件,我有一個這樣的形式 -自動從Web上的圖像URL將圖像導入到表單中?

<form action="" method="post" enctype="multipart/form-data"> 
    <table class="grid" width="400"> 
     <tr class="r1"> 
      <td><label for="name">Title *:</label>  </td> 
      <td><input name="name" type="text" id="name" value="<?php echo $name?>" size="40" />  </td> 
     </tr> 
     <tr class="r1"> 
      <td><label for="tags">Tags*:</label></td> 
      <td><input name="tags" type="text" id="tags" value="<?php echo View::escape($wallpaper->tags) ?>" size="40" />  </td> 
     </tr> 

     <tr class="r1"> 
      <td><label for="img">Wallpaper*:</label></td> 
      <td><input type="file" name="img" id="img" />  </td> 
     </tr> 


      <td><input type="submit" name="upload_wallpaper" id="upload_wallpaper" value="Submit" /> 

     </tr> 
    </table> 
</form> 

現在我想通過一個URL鏈接形式的這一部分加載圖像 -

<tr class="r1"> 
     <td><label for="img">Wallpaper*:</label></td> 
     <td><input type="file" name="img" id="img" />  </td> 
    </tr> 

之後我會手動提交表格之後..我只想通過輸入網址,如* www.mysite.com/images/my.jpg *通過網址上傳與出選擇瀏覽並選擇一個圖像文件..我嘗試了很多,但我沒有找到解決方案..謝謝!無論如何?

回答

1

將輸入類型從文件更改爲文本框。然後在後端,使用curl或類似的東西來下載輸入的圖像文件(如果存在的話)。

if (isset($_POST['img'])) { 
    $url = $_POST['img']; 

    // Curl the URL 
    $ch = curl_init ($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 
    curl_close ($ch); 

    // Save it to disk 
    $savePath = "/somewhere-on-your-disk/filename.jpg"; 
    $fp = fopen($savePath,'x'); 
    fwrite($fp, $result); 
    fclose($fp); 
} 
+0

是的,我試過像這樣...但它不工作..btw如何以捲曲方式做到這一點?你可以幫我嗎? – user1020876

+0

我編輯了我的答案..但你也應該添加一些錯誤檢查。在嘗試將其保存到磁盤之前,請確保文件存在 – Patrick