JohnHartsey123 JohnHartsey123 - 7 months ago 52
PHP Question

PHP Directory Paths Dilema

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


$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>
<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.


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