簡體   English   中英

使用PHP檢查遠程文件是否存在

[英]Checking existence of a remote file using PHP

我試圖找到一種最佳的解決方案,以節省性能,內存使用量等,以檢查文件是否存在於不同的域中。 在我的情況下,文件是XML ,大小可以在10KB到10MB之間。

哪一種是最佳使用方式? 如果您有更好的方法,我會很樂意使用它。

謝謝

卷曲

$ch = curl_init("http://www.example.com/hello.xml");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);

FOPEN

$url = "http://www.example.com/hello.xml";

if (@fopen($url, "r")) {
   echo "File Exists";
} else {
   echo "Can't Connect to File";
}
$opts = array('http' =>
  array(
    'method'  => 'HEAD'
  )
);

$context  = stream_context_create($opts);

$result = fopen('http://example.com/submit.php', 'rb', false, $context);

手冊中的示例(但已簡化)

然后使用stream_get_meta_data()獲取響應標頭,類似

$meta = stream_get_meta_data($result);
var_dump($meta['wrapper_data']);

暫無
暫無

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

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