2010-09-03 94 views
1

我想用我的強制下載腳本從任何網站下載任何文件。簡單的腳本,它只是提示下載窗口將文件保存到桌面。我只是將?path = http://www.google.com/images/whatever/file.jpg傳遞給url,腳本觸發下載。強制從任何頁面下載文件!檢查文件是否可讀?

但是,該腳本當然不適用於每個頁面。大多數時候我沒有權限這麼做!

HTTP請求失敗! HTTP/1.0 403禁止在...

是否有一種方法,我可以檢查是否強制從某個域下載工作與否。 像is_readable()什麼的。

問候亞光

編輯:

<?php 
    error_reporting(E_ALL); 

    if(isset($_GET['p'])) $path = $_GET['p']; 
    else header('Location:' . 'mypage'); 

    $file = $path; 
    //header('Location:' . $file); 
    // 
    header("Cache-Control: no-cache"); 
    header("Expires: -1"); 
    header("Content-Type: application/octet-stream;"); 
    header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";"); 
    header("Content-Transfer-Encoding: binary"); 
    //header("Content-Length: " . filesize($file)); 
    //echo file_get_contents($file); 
    echo readfile($file); 
    ?> 
+0

編輯在那裏!我甚至不確定是否可以使用php腳本從任何服務器下載任何文件。那將是我的目標。我很想只傳遞任何網址(指向任何文件),例如http://goblueridgecard.com/blog/files/2008/04/blue-ridge-flower.jpg和我的腳本提示下拉窗口並將其正確保存到桌面。那可能嗎? 例如,我想抓住一個Facebook圖像的圖像URL,將它傳遞給我的腳本,並將圖像下載到我的高清。 – matt 2010-09-03 01:09:03

+0

我的答案直接回答你的問題。使用我慷慨提供的WebPage類,它將驗證該URL是否合法,下載該項目,併爲您提供框架*輕鬆*確定它是否被禁止。 – 2010-09-03 01:16:06

回答

1

我爲我的mediawiki word count項目創建了這個類。

// Copyright PHPExperts.pro 
// License: Any user on Stackflow may use this code under the BSD License. 

/** 
* Web page datatype that holds all the various parts 
* and info about a web page. 
*/ 
class WebPage 
{ 
    public $url; 
    public $headers; 
    public $body; 
    public $text; 


    public function __construct($url) 
    { 
     // 1. Bail out now if the CURL extension is not loaded. 
     if (!in_array('curl', get_loaded_extensions())) 
     { 
      throw new Exception(WebPageException::MISSING_CURL); 
     } 

     // 2. Make sure the URL is valid. 
     self::ensureValidURL($url); 

     // 3. Store the URL. 
     $this->url = $url; 
    } 

    /** 
    * Determine if a URL is valid. 
    * 
    * @param string $url 
    * @returns true if the URL is a string and is a valid URL. False, otherwise. 
    */ 
    public static function isURLValid($url) 
    { 
     return (is_string($url) && 
       filter_var($url, FILTER_VALIDATE_URL) !== false); 
    } 

    public static function ensureValidURL($url) 
    { 
     if (!self::isURLValid($url)) 
     { 
      throw new WebPageException(WebPageException::INVALID_URL, array($url)); 
     } 
    } 

    // captureHeader() donated by [email protected], 
    // via http://us.php.net/curl_setopt_array 
    private function captureHeader($ch, $header) 
    { 
     $this->headers[] = $header; 
     return strlen($header); 
    } 

    public function fetchURL() 
    { 
     $ch = curl_init(); 
     curl_setopt_array($ch, array(CURLOPT_URL => $this->url, 
            CURLOPT_RETURNTRANSFER => 1, 
            CURLOPT_HEADERFUNCTION => array($this, 'captureHeader'), 
            CURLOPT_TIMEOUT => 5, 
            ) 
         ); 

     $data = curl_exec($ch); 
     curl_close($ch); 

     if ($data === false || is_null($data) || $data == '') 
     { 
      throw new WebPageException(WebPageException::BLANK_URL, array($this->url)); 

     } 

     // TODO: Need to handle HTTP error messages, such as 404 and 502. 
     $this->body = $data; 
       // Uses code from [email protected] 
       $this->text = remove_HTML($data); 
    } 
} 

在運行WebPage::captureHeader()你再剛剛經歷$this->headers的foreach,如果你沒有找到HTTP/1.0 403禁止,你是好去。

這徹底回答你的問題,所以我期待信用。

3

如果你的腳本使用捲曲下載該文件,你可以使用curl_getinfo和文件傳遞到用戶之前檢查的CURLINFO_HTTP_CODE。