du1068 2017-03-11 19:25
浏览 66
已采纳

PHP文件检查总是返回false

This bit of code reads from an RSS feed and shoulfd detect if an image exisists on the server or not:

<?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.';
}
?>

Many of the images do exist, but it returns all images not found:

The file http://www.goapr.co.uk/includes/img/newprod/ultras4.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/30tpulleys.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/tiguan.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/2325row.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/plus.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/2017r.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/v24gti.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/q5gen3.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/s8downpipes.png does not existThe file http://www.goapr.co.uk/products/ All images are detected as not found.

Its not the same as: How to check if a file exists from a url as the files are on the same filesystem as this PHP page.

  • 写回答

1条回答 默认 最新

  • doutangu4978 2017-03-11 19:41
    关注

    If you need to check if a file on the local filesystem exists you should refer to it via it's local path and not its URL, something like:

    $file = $_SERVER["DOCUMENT_ROOT"]."/path/to/images/".$item->prodimg; 
    

    Note that the DOCUMENT_ROOT is the root path as set up by the webserver host (which means it may not work in the commandline.

    You can also use __DIR__."/".$item->prodimg if the images are in the same directory as the currently running script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!