这个问题困扰了我一段时间了。我试图用PHP加载图像,但有jpg和jpg图像,当我尝试用jpg加载图像时,jpg图像找不到(显然)
$img1 = '<img src="imgs/'.$firstImage.'.jpg"></img>';
$img2 = '<img src="imgs/'.$secondImage.'.jpg"></img>';
$img3 = '<img src="imgs/'.$thirdImage.'.jpg"></img>';
$img4 = '<img src="imgs/'.$fourthImae.'.jpg"></img>';
知道如何解决这个问题吗?
我试着使用if_exists
,但效果不佳
if (file_exists('http://website_url.nl/imgs/'.$firstImage.'.jpg')) {
$img1 = '<img src="imgs/'.$firstImage.'.jpg"></img>';
} else {
$img1 = '<img src="imgs/'.$firstImage.'.JPG"></img>';
}
现在的问题是,它找不到具有常规jpg的图像。例如,我试图加载23.jpg
,但脚本尝试加载23.JPG
,因为文件23.jpg
不存在,而它确实存在,并且被放置在文件夹中,而23.JPG
不存在。它可以处理JPG文件。例如,发现DOC_202.JPG
是因为DOC_202.jpg
不存在,所以它加载JPG
。但对于jpg
,它不会起作用。有什么解决方案吗?
谨致问候,
只需检查本地目录中的文件,不要使用url
if(file_exists('imgs/'.$firstImage.'.jpg')) {
大多数url包装器不支持stat(),请参阅http://www.php.net/manual/en/function.file-exists.php(最后是关于php5的蓝色注释)和http://www.php.net/manual/en/wrappers.php
编辑:
附言:当使用windows时,目录分隔符是反斜杠"''",在linux上是正斜杠"/"。如果需要全局解决方案,请参阅全局常数DIRECTORY_SEPARATOR
。
http://php.net/manual/en/function.file-exists.php
因此,如果您将file_exists参数更改为本地地址,它应该可以工作。例如:
if(file_exists('/webdirectory/public_html/project/images/' . $image1 . '.jpg')){
//do stuff
}
file_exists的大小写敏感度取决于它所在的文件系统。在unix服务器上,文件大小写很重要,file_exist也很重要。
PHP手册中的这条注释建议了一种通过URL进行检查的方法:http://php.net/manual/en/function.file-exists.php#75064