代码之家  ›  专栏  ›  技术社区  ›  matt

强制从任何页面下载文件!检查文件是否可读?

  •  1
  • matt  · 技术社区  · 14 年前

    我想用我的forcedownload脚本从任何网站下载任何文件。简单的脚本,它只提示下载窗口将文件保存到桌面。我只是通过?路径= http://www.google.com/images/whatever/file.jpg 到URL,脚本启动下载。

    然而,这个脚本当然不能适用于每一页。大多数时候我没有权限这样做!

    HTTP请求失败!HTTP/1.0 403在…中被禁止。

    有没有一种方法可以检查从某个域强制下载是否有效? 就像是可读的()之类的。

    关于马特

    编辑:

        <?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);
        ?>
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Theodore R. Smith    14 年前

    我为我的Mediawiki字数项目创建了这个类。

    // 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 bendavis78@gmail.com,
        // 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 php@wizap.dom
                    $this->text = remove_HTML($data);
        }
    }
    

    你跑完以后 WebPage::captureHeader() 你就这么向前冲过去 $this->headers 如果你没有发现HTTP/1.0 403被禁止,你就可以走了。

    这完全回答了你的问题,所以我希望得到赞扬。

        2
  •  3
  •   Viktor Stískala    14 年前

    如果脚本使用curl下载文件,则可以使用 curl_getinfo 并在将文件传递给用户之前检查curlinfo_http_代码。