dq1685513999 2016-11-26 18:38
浏览 10
已采纳

PHP目录路径困境

The Problem

The problem is that my php script will not detect some paths with / at the front. But some would. Hard to explain, you will see in the code.

The File Tree

This is my File Tree

Main Folder

  1. Image Folder (inside contains another folder called profileimages, which contains the picture i am looking for.)
  2. index.php
  3. navbar.php
  4. index.css
  5. navbar.css

CODE

$filename = "/image/profileimages/" . $_SESSION['profileid'] . ".jpg"; /*Sets the File Path*/
    echo "
    <div id=loginprofilebox>";
    if (file_exists($filename)) {
        echo "<img src=/image/profileimages/" . $_SESSION['profileid'] . ".jpg alt=PROFILEIMG>";
    } else {
        echo "<img src=/image/profileimages/defaultimage.png alt=UIMG>";
    }
    echo "<span id=loginprofilebox_details>
        <h2><u>$_SESSION[profilename]</u></h2>
        <h3 style=display:inline;>$_SESSION[profileid]</h3>
    ";

What I have so far

Right now, the second part of the code works, the statements inside the if statement. For example, echo "<img src=/image/profileimages/" . $_SESSION['profileid'] . ".jpg alt=PROFILEIMG>"; works. However, it would nto activate without the file path working properly. If I put this instead of the current file path, $filename = "image/profileimages/" . $_SESSION['profileid'] . ".jpg"; /*Sets the File Path*/ Then it would work. On the Homepage. When i navigate to other pages, it no longer becomes relative to the image. But to recap, the statements inside the if statement would still work, but the filepath itself would not work.

  • 写回答

1条回答 默认 最新

  • doty58493 2016-11-26 18:51
    关注

    Hi john to check file you need to use the absolute file path..

    some thing like this below:

    if ( file_exists( dirname(__FILE__)."/image/profileimages/" . $_SESSION['profileid'] . ".jpg") ) {
       //  true  
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用