首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP中的file_exists不像预期的那样工作

PHP中的file_exists不像预期的那样工作
EN

Stack Overflow用户
提问于 2015-03-05 21:56:50
回答 2查看 675关注 0票数 0

对于用户的个人资料在我的网站上,他们被允许上传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,它总是说文件不存在。下面是我使用的代码:

代码语言:javascript
复制
<?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
}   
?>

在上面的示例中,用户名只是用户的用户名。所以我想知道为什么这个代码不能工作?(谢谢:)

编辑这里是我的整体文件路径:

代码语言:javascript
复制
C:\wamp\www\postin'\profiles\pictures\image.png

但当我输入时:

代码语言:javascript
复制
\pictures\image.png

它将无法工作,图像不会显示!:(顺便说一句,我的目录结构:

代码语言:javascript
复制
Postin'
  Profiles
    Pictures
      image.png
EN

回答 2

Stack Overflow用户

发布于 2015-03-05 22:18:26

来自php.net评论

在使用file_exists时,似乎无法做到:

代码语言:javascript
复制
<?php 
foreach ($possibles as $poss) 
{ 
    if ( file_exists(SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg') ) 
    { 
        // exists 
    } 
    else 
    { 
        // not found 
    } 
} 
?> 

所以你必须做:

代码语言:javascript
复制
<?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系统上是这样的

我不确定是因为串接还是里面有一个常数,我正要跑去测试.

票数 1
EN

Stack Overflow用户

发布于 2015-03-05 22:18:31

嗯,如果你不知道确切的扩展-不要使用if/else语句。你不能涵盖所有可能的小写/大写字母组合.

就我个人而言,我建议把图片转换成你最喜欢的格式,当用户上传的时候,这样你就知道你在找什么了!

如果出于任何原因这是不可能的,您可以使用glob()查找用户上传的任何文件。

代码语言:javascript
复制
$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);
} 

但是,这需要处理多个文件.不..。转换它,保存它,使用它!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28888536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档