簡體   English   中英

PHP Curl在下載之前檢查文件是否存在

[英]PHP Curl check for file existence before downloading

我正在編寫一個PHP程序,從后端下載pdf並保存到本地驅動器。 現在如何在下載之前檢查文件是否存在?

目前我正在使用curl(請參閱下面的代碼)進行檢查和下載,但它仍然會下載大小為1KB的文件。

$url = "http://wedsite/test.pdf";
$path = "C:\\test.pdf;"
downloadAndSave($url,$path);

function downloadAndSave($urlS,$pathS)
    {
        $fp = fopen($pathS, 'w');

        $ch = curl_init($urlS);

        curl_setopt($ch, CURLOPT_FILE, $fp);
        $data = curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo $httpCode;
        //If 404 is returned, then file is not found.
        if(strcmp($httpCode,"404") == 1)
        {
            echo $httpCode;
            echo $urlS; 
        }

        fclose($fp);

    }

我想在下載之前檢查文件是否存在。 知道怎么做嗎?

您可以使用單獨的curl HEAD請求執行此操作:

curl_setopt($ch, CURLOPT_NOBODY, true);
$data = curl_exec($ch);

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

當您真正想要下載時,您可以使用設置NOBODYfalse

由於您使用HTTP來獲取Internet上的資源,您真正想要檢查的是返回碼是404。

在某些PHP安裝中,您可以直接使用file_exists($url) 但是,這並不適用於所有環境。 http://www.php.net/manual/en/wrappers.http.php

這里的函數很像file_exists但對於URL,使用curl:

<?php function curl_exists()
  $file_headers = @get_headers($url);
  if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
  }
  else {
    $exists = true;
  }
} ?>

來源: http//www.php.net/manual/en/function.file-exists.php#75064

有時CURL擴展沒有安裝PHP。 在這種情況下,您仍然可以在PHP核心中使用套接字庫:

<?php function url_exists($url) {
       $a_url = parse_url($url);
       if (!isset($a_url['port'])) $a_url['port'] = 80;
       $errno = 0;
       $errstr = '';
       $timeout = 30;
       if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
           $fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
           if (!$fid) return false;
           $page = isset($a_url['path'])  ?$a_url['path']:'';
           $page .= isset($a_url['query'])?'?'.$a_url['query']:'';
           fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
           $head = fread($fid, 4096);
           $head = substr($head,0,strpos($head, 'Connection: close'));
           fclose($fid);
           if (preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)) {
            $pos = strpos($head, 'Content-Type');
            return $pos !== false;
           }
       } else {
           return false;
       }
   } ?>

來源: http//www.php.net/manual/en/function.file-exists.php#73175

可以在這里找到更快的功能: http//www.php.net/manual/en/function.file-exists.php#76246

在下載功能之前調用它並完成:

<?php function remoteFileExists($url) {
    $curl = curl_init($url);

    //don't fetch the actual page, you only want to check the connection is ok
    curl_setopt($curl, CURLOPT_NOBODY, true);

    //do request
    $result = curl_exec($curl);

    $ret = false;

    //if request did not fail
    if ($result !== false) {
        //if request was ok, check response code
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  

        if ($statusCode == 200) {
            $ret = true;   
        }
    }

    curl_close($curl);

    return $ret;
}

?>

在上面的第一個示例中,$ file_headers [0]可能包含除“HTTP / 1.1 404 Not Found”之外的其他內容,例如:

HTTP/1.1 404 Document+%2Fdb%2Fscotbiz%2Freports%2FR20131212%2Exml+not+found

所以使用其他測試很重要,例如正則表達式,因為'=='不可靠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM