【问题标题】:file_exists in PHP is not working as expectedPHP 中的 file_exists 未按预期工作
【发布时间】: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


【解决方案1】:

来自 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 和 apache 2.2.3 的 Windows 系统上是这样的

不确定是由于串联还是其中有一个常数,我正要逃跑并测试...

【讨论】:

    【解决方案2】:

    好吧,如果您不知道确切的扩展名 - 不要使用 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);
    } 
    

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

    【讨论】:

    • 切换图片类型是不是很糟糕?
    • @michaeljones 它取决于。例如,如果源不是 jpg,JPG 会降低质量。对于所有上传的图像,我个人更喜欢 PNG,因为它是一种无损压缩格式,因此没有人能分辨出来。它可能更大,但现在磁盘空间通常不是问题。所以,对于 PHP,你总是可以使用这样的方法:coderzone.org/library/…。过滤用户输入还可以确保没有人可以上传大文件或错误的分辨率等。
    猜你喜欢
    • 2018-12-22
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多