PHP中存在的文件“”未按预期工作
对于我的网站上的用户配置文件,他们可以上载1个配置文件图像。它可以是PNG、JPG、JPEG或GIF。现在我的问题是显示图像。基本上我想看看它有什么类型的文件扩展名,然后相应地显示文件。我试图通过PHP中的file_exists函数实现这一点,但它似乎不起作用!:( 如果我键入URL(使用.png作为示例) “/profiles/pictures/username.png 在我的URL栏中,它将显示该用户的图像 C:/wamp/www/postin'/profiles/pictures/username.png 在URL栏中,它将显示该用户的图像。现在我的问题是,如果我对其中任何一个进行PHP文件检查,它总是说该文件不存在。下面是我使用的代码:PHP中存在的文件“”未按预期工作,php,html,file-exists,Php,Html,File Exists,对于我的网站上的用户配置文件,他们可以上载1个配置文件图像。它可以是PNG、JPG、JPEG或GIF。现在我的问题是显示图像。基本上我想看看它有什么类型的文件扩展名,然后相应地显示文件。我试图通过PHP中的file_exists函数实现这一点,但它似乎不起作用!:( 如果我键入URL(使用.png作为示例) “/profiles/pictures/username.png 在我的URL栏中,它将显示该用户的图像 C:/wamp/www/postin'/profiles/pictures/user
<?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
}
?>
但当我输入时:
\pictures\image.png
它将不工作,图像不显示!:(
顺便说一句,我的目录结构:
Postin'
Profiles
Pictures
image.png
来自php.net的评论
当使用文件_存在时,您似乎无法执行以下操作:
<?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
}
}
?>
然后一切都会好起来的
至少在运行PHP5.2.5和apache 2.2.3的Windows系统上是这样
我不确定这是因为串联还是因为其中有一个常数,我要跑开测试一下…好吧,如果你不知道确切的长度-不要使用
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);
}
$profileimagepath=“C:/wamp/www/postin'/profiles/pictures/”;
$profileimageurl=”http://localhost/postin“/简介/图片/”;
$files=glob($profileimagepath.$username..*);
foreach($files作为$file){
echo“找到一个图像:
”;
echo$文件。“
”;
echo“外部地址为:”.str_replace($profileimagepath,$profileimageurl,$file);
}
但是,这需要处理多个文件,并且…不…转换、保存、使用它!为什么文件夹名称中有撇号?只有名称:)虽然我没有使用撇号测试它,但它仍然不起作用。这个撇号会让生活变得非常艰难。
.JPG
,或.JPG
,或.JPG
,等等呢?为什么要连接两个静态字符串,比如“.JPG”
,而不是简单地使用.JPG?”
?为什么不在数据库中存储用户配置文件映像的路径,而不依赖重复且代价高昂的IO操作?在PHP中,$profileimagepath=“C:/wamp/www/postin'/profiles/pictures/”;永远无法返回您的映像或任何文件夹。想象一下,PHP在您的服务器上运行(如果它是虚拟wamp服务器)现在,尝试在服务器上找到一个路径,该路径不存在,C:/wamp/www/po..
。$files=glob($\u server[“DOCUMENT\u ROOT]”.“/postin'/profiles/pictures/*”);var\u dump($files);
现在您的$files
必须返回一些东西,如果没有:var\u dump($\u服务器['DOCUMENT_ROOT']);
//这返回了什么?切换图像类型不是很糟糕吗?@michaeljones这取决于。例如,如果源代码不是JPG,JPG会降低质量。我个人更喜欢所有上传的图像都使用PNG,因为它是一种无损耗压缩格式,因此没有人能看出区别。它可能更大,但通常磁盘空间不是I因此,对于PHP,您可以始终使用这样的方法:。过滤用户输入还可以确保没有人可以上传到大文件或错误的分辨率等。
$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);
}