PHP:file_exists()无法正常工作。

问题描述 投票:1回答:1

file_exists()似乎对我不起作用。它默认为else语句blank_user.png。但它确实显示没有file_exists()检查我的代码中的最后一个img。我无法弄清楚出了什么问题。

<?php 
    $filename = "/project/images/user_image/" . $username . ".png";
    if(file_exists($filename)){ ?>
    <img src = "<?php echo '/project/images/user_image/' . $username . '.png';?>" alt = "User Pic" height = 280 width = 280 />

<?  
    } else {  ?>
    <img src = "<?php echo '/project/images/user_image/default/blank_user.png';?>" alt = "User Pic" height = 280 width = 280 />
<? } ?>
</br>
    <img src = "<?php echo '/project/images/user_image/' . $username . '.png';?>" alt = "User Pic" height = 280 width = 280 />
?>
php
1个回答
1
投票

请注意,file_exists()采用绝对路径。它不会破坏你,你只是传递一个不存在的错误路径。你在寻找的是一条相对的道路。

在您当前的解决方案中,您正在文件系统的根目录中寻找/project,我几乎保证您不需要它。

要使用相对路径,您需要使用__DIR__ . '/../relative/path/here',其中__DIR__是当前正在执行的PHP文件的目录。

© www.soinside.com 2019 - 2024. All rights reserved.