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.

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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)