对于用户的个人资料在我的网站上,他们被允许上传1个人资料图像。它可以是PNG、JPG、JPEG或GIF。现在我的问题是显示图像。基本上,我想看看它有什么类型的文件扩展名,然后显示相应的文件。我正试图用PHP中的file_exists函数来实现这一点,但它似乎不起作用!
如果我键入URL (示例使用.png )
http://localhost/postin'/profiles/pictures/username.png
在我的URL栏中,它将显示该用户的图像。如果我键入文件路径
C:/wamp/www/postin'/profiles/pictures/username.png
在URL栏中,它将显示该用户的图像。现在我的问题是,如果我对其中任何一个做了PHP file_check,它总是说文件不存在。下面是我使用的代码:
<?php
$profileimagepath = "C:/wamp/www/postin'/profiles/pictures/";
$profileimageurl = "http://localhost/postin'/profiles/pictures/";
if (file_exists($profileimagepath.$username."."."png")) { ?>
<img src=<?php $profileimageurl.$username."."."png"; ?> id="accountpictureholderspecs">
<?php
}
elseif (file_exists($profileimagepath.$username."."."jpg")) { ?>
<img src=<?php $profileimageurl.$username."."."jpg"; ?> id="accountpictureholderspecs">
<?php
}
elseif (file_exists($profileimagepath.$username."."."jpeg")) { ?>
<img src=<?php $profileimageurl.$username."."."jpeg"; ?> id="accountpictureholderspecs">
<?php
}
elseif (file_exists($profileimagepath.$username."."."gif")) { ?>
<img src=<?php $profileimageurl.$username."."."gif"; ?> id="accountpictureholderspecs">
<?php
}
else { ?>
<img src="http://localhost/postin'/images/profileimage.png" id="accountpictureholderspecs">
<?php
}
?>在上面的示例中,用户名只是用户的用户名。所以我想知道为什么这个代码不能工作?(谢谢:)
编辑这里是我的整体文件路径:
C:\wamp\www\postin'\profiles\pictures\image.png但当我输入时:
\pictures\image.png它将无法工作,图像不会显示!:(顺便说一句,我的目录结构:
Postin'
Profiles
Pictures
image.png发布于 2015-03-05 22:18:26
来自php.net评论
在使用file_exists时,似乎无法做到:
<?php
foreach ($possibles as $poss)
{
if ( file_exists(SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg') )
{
// exists
}
else
{
// not found
}
}
?> 所以你必须做:
<?php
foreach ($possibles as $poss)
{
$img = SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg'
if ( file_exists($img) )
{
// exists
}
else
{
// not found
}
}
?> 然后一切都会好起来的。
至少在这个运行php 5.2.5和Apache2.2.3的Windows系统上是这样的
我不确定是因为串接还是里面有一个常数,我正要跑去测试.
发布于 2015-03-05 22:18:31
嗯,如果你不知道确切的扩展-不要使用if/else语句。你不能涵盖所有可能的小写/大写字母组合.
就我个人而言,我建议把图片转换成你最喜欢的格式,当用户上传的时候,这样你就知道你在找什么了!
如果出于任何原因这是不可能的,您可以使用glob()查找用户上传的任何文件。
$profileimagepath = "C:/wamp/www/postin'/profiles/pictures/";
$profileimageurl = "http://localhost/postin'/profiles/pictures/";
$files = glob($profileimagepath . $username . ".*");
foreach($files AS $file){
echo "Found an image: <br />";
echo $file. "<br />";
echo "External-Adress is: " . str_replace($profileimagepath, $profileimageurl, $file);
} 但是,这需要处理多个文件.不..。转换它,保存它,使用它!
https://stackoverflow.com/questions/28888536
复制相似问题