PHP Directory Paths Dilema

时间:2016-11-26 18:38:50

标签: 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 个答案:

答案 0 :(得分:0)

嗨约翰检查文件你需要使用绝对文件路径..

如下所示:

if ( file_exists( dirname(__FILE__)."/image/profileimages/" . $_SESSION['profileid'] . ".jpg") ) {
   //  true  
 }