duanhuantong8278 2015-03-05 21:56
浏览 19

PHP中的file_exists无法按预期工作

For the user's profile on my website they are allowed to upload 1 profile image. It can be PNG, JPG, JPEG, or GIF. Now my problem is displaying the images. Basically I would like to see what type of file extension it has, and then display the file accordingly. I am trying to achieve this with the file_exists function in PHP, but it does not seem to work! :(

If I type the URL (uses .png for the example)

http://localhost/postin'/profiles/pictures/username.png

into my URL bar it will display that user's image. Then if I type in the file path

C:/wamp/www/postin'/profiles/pictures/username.png

into the URL bar it will display that user's image. Now my problem is if I do PHP file_check on either of those it always says the file does NOT exist. Here is the code I am using:

<?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
}   
?>

And in the example above username is simply the user's username. So what I am wondering is why would this code not be working? Thank You!! :)

EDIT Here is my overall file path:

C:\wamp\www\postin'\profiles\pictures\image.png

Yet when I type in:

\pictures\image.png

It will not work, the image does not display! :( BTW, my directory structure:

Postin'
  Profiles
    Pictures
      image.png
  • 写回答

2条回答 默认 最新

  • douhuiwan5141 2015-03-05 22:18
    关注

    From php.net comment

    When using file_exists, seems you cannot do:

    <?php 
    foreach ($possibles as $poss) 
    { 
        if ( file_exists(SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg') ) 
        { 
            // exists 
        } 
        else 
        { 
            // not found 
        } 
    } 
    ?> 
    

    so you must do:

    <?php 
    foreach ($possibles as $poss) 
    { 
        $img = SITE_RANGE_IMAGE_PATH .$this->range_id .'/ '.$poss .'.jpg' 
        if ( file_exists($img) ) 
        { 
            // exists 
        } 
        else 
        { 
            // not found 
        } 
    } 
    ?> 
    

    Then things will work fine.

    This is at least the case on this Windows system running php 5.2.5 and apache 2.2.3

    Not sure if it is down to the concatenation or the fact theres a constant in there, i'm about to run away and test just that...

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题