【发布时间】:2015-05-07 10:52:21
【问题描述】:
对于我网站上的用户个人资料,他们可以上传 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
}
?>
在上面的示例中,username 只是用户的用户名。所以我想知道为什么这段代码不起作用?谢谢!! :)
编辑 这是我的整体文件路径:
C:\wamp\www\postin'\profiles\pictures\image.png
当我输入时:
\pictures\image.png
它不起作用,图像不显示! :( 顺便说一句,我的目录结构:
Postin'
Profiles
Pictures
image.png
【问题讨论】:
-
为什么文件夹名称中有撇号?
-
只是名称 :) 我已经在没有撇号的情况下对其进行了测试,但它仍然无法正常工作。
-
撇号会让生活变得非常艰难
-
.JPG、.Jpg、.jPg等呢?为什么要连接两个像"."."jpg"这样的静态字符串,而不是简单地使用".jpg"?为什么不将用户头像的路径存储在数据库中,而不是依赖重复且成本高昂的 IO 操作? -
在 PHP 中
$profileimagepath = "C:/wamp/www/postin'/profiles/pictures/";永远无法返回您的图像或任何文件夹。想象一下,PHP 在您的服务器上运行(如果它是一个虚拟 wamp 服务器),现在它试图(在服务器上)找到一个带有C:/wamp/www/po..的路径,但它不存在......尝试这样的事情怎么样:@ 987654333@ 现在你的$files必须返回一些东西,如果没有:var_dump($_SERVER['DOCUMENT_ROOT']);// 这是什么返回?
标签: php html file-exists