PHP file_exists无法正常工作 [英] PHP file_exists not working

查看:71
本文介绍了PHP file_exists无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使个人资料照片与我正在研究的网站上的评论列表相对应.如果他们没有个人资料照片,那么我会显示标准图像,不幸的是,即使存在,该图像也会始终显示为标准图像,而不是个人资料.这是代码:

$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
    if(file_exists($reviewerPic)){
        $reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
    }else {
        $reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}

相当通用的代码,但似乎不起作用!它只是不断显示背景图像...

关于file_exists为什么行不通的任何想法?

解决方案

您正在使用的函数 file_exists 使用物理路径,您需要提供的参数应该是可以在其中找到文件的服务器上的地址,而不是url

您应该有类似

的信息

/home/var/www/images/

代替

http://www.[URL].co.uk/images/

因此,您需要检查文件是否在本地服务器上存在,然后您可以使用URL将其公开(在img src中)

您可以在手册页上看到此功能仅适用于某些 URL包装器,因此最好使用路径而不使用url(我想这取决于允许url fopen设置)

I'm trying to make profile photos show up against a list of reviews on a site I'm working on. If they don't have a profile photo I have a standard image to show instead, unfortunately the image always goes to the standard image rather than the profile even if it exists. Heres the code:

$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
    if(file_exists($reviewerPic)){
        $reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
    }else {
        $reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}

Pretty generic code but it doesn't seem to work! It just keeps showing the background image...

Any ideas on why file_exists wouldn't be working?

解决方案

The function you are using, file_exists, uses physical paths, the parameter you need to provide should be the address on that server where the file can be found, and not an url

Sou you should have something like

/home/var/www/images/

instead of

http://www.[URL].co.uk/images/

So you need to check if the file exists on the server locally and after that you can use an url to make it available to the public (in img src)

You can see on the man page that this function only works with some URL wrappers, so it is better to use paths and not urls (I guess it depends on allow url fopen setting)

这篇关于PHP file_exists无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆