這段代碼從一個RSS feed讀取,如果服務器或不上的圖像exisists shoulfd檢測:PHP文件檢查總是返回false
<?php
$feedURL = 'http://www.goapr.com/news/category/product-release/feed/';
$sxml = @simplexml_load_file($feedURL);
if($sxml){
$i=0;
foreach($sxml->channel as $channel){
foreach($channel->item as $item){
//if($i==6){break;}
if($item->prodimg=="~"){break;}
if($item->prodpage=="~"){break;}
$i += 1;
$file = 'http://www.goapr.co.uk'. $item->prodimg;
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
}
}
}else{
echo 'Sorry there was an error. The recent products will return shortly.';
}
?>
許多圖像確實存在,但它返回的所有圖片未找到:
文件http://www.goapr.co.uk/includes/img/newprod/ultras4.png不existThe文件http://www.goapr.co.uk/includes/img/newprod/30tpulleys.png不existThe文件http://www.goapr.co.uk/includes/img/newprod/tiguan.png沒有existThe文件http://www.goapr.co.uk/includes/img/newprod/2325row.png沒有existThe文件http://www.goapr.co.uk/includes/img/newprod/plus.png不existThe文件http://www.goapr.co.uk/includes/img/newprod/2017r.png不existThe文件http://www.goapr.co.uk/includes/img/newprod/v24gti.png不existThe文件http://www.goapr.co.uk/includes/img/newprod/q5gen3.png不存在文件http://www.goapr.co.uk/includes/img/newprod/s8downpipes.png不存在文件http://www.goapr.co.uk/products/ 所有圖像被檢測爲未找到。
它與How to check if a file exists from a url不一樣:How to check if a file exists from a url由於文件與此PHP頁面位於同一文件系統中。
file_exists只能在本地文件系統我個人使用cURL打開一個連接並獲取信息並檢查響應是否爲404,儘管檢查'fopen'是否成功通常就足夠了。 – apokryfos
嗨,謝謝。這些文件和這個PHP頁面在同一個文件系統上,這有幫助嗎? –
可能的重複[如何檢查文件是否存在從URL](http://stackoverflow.com/questions/7684771/how-check-if-file-exists-from-the-url) – Ruben