这个问题困扰了我一段时间了。我试图在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不存在。例如,之所以找到DOC_202.JPG,是因为DOC_202.jpg不存在,所以它加载了JPG。但是有了jpg,它就行不通了。有什么解决办法吗?
向您致以亲切的问候,
发布于 2016-05-10 11:25:05
只需检查本地目录中的文件,不要使用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
编辑:
PS:附带注意,在使用windows时,目录分隔符是反斜杠“\”,而在linux上则是正斜杠“/”。如果需要全局解决方案,请参见全局常量DIRECTORY_SEPARATOR。
发布于 2016-05-10 11:25:11
函数file_exists检查本地系统是否存在文件,而不是外部URL。
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_exists也是如此。
PHP手册中的这个注释提供了一种通过URL进行检查的方法:http://php.net/manual/en/function.file-exists.php#75064
https://stackoverflow.com/questions/37136919
复制相似问题