Page 1 of 1
file_exists() problem
Posted: Mon Jun 13, 2005 10:37 am
by frolicols
Hi, newbie here, learning as I go along. I'm having a problem with the file_exists() function in that it always returns false even though I know the file DOES exist. The php page is in my root folder, and the image file I'm trying to test exists in subfolder /images/photos/. See the code below
Code: Select all
$id = $_GET['id'];
$query1 = "SELECT * FROM Players WHERE player_id = $id";
$result1 = mysql_query($query1, $conn);
$filename = "/images/photos/$id.jpg";
if (file_exists($filename)) { TRUE } ELSE {FALSE}
I thought it was something to do with the file path maybe, but to be honest I've looked everywhere and can't work out what's wrong!!

Posted: Mon Jun 13, 2005 10:53 am
by programmermatt
Try
Code: Select all
$id = $_GET['id'];
$query1 = "SELECT * FROM Players WHERE player_id = $id";
$result1 = mysql_query($query1, $conn);
$filename = "/images/photos/$id.jpg";
echo $filename;
if (file_exists($filename)) { TRUE } ELSE {FALSE}
Make sure that the file does exist in that path relative to the executing php script.
Also, if you are on linux $filename = "images/photos/{$id}.jpg", otherwise it will look in the '/'
Posted: Mon Jun 13, 2005 11:01 am
by Skara
Yeah, file_exists uses the real root, not the website root.
In other words, it would be more like...
/home/yourname/public_html/images/photos/$id.jpg
Posted: Tue Jun 14, 2005 3:34 am
by frolicols
Skara wrote:Yeah, file_exists uses the real root, not the website root.
In other words, it would be more like...
/home/yourname/public_html/images/photos/$id.jpg
The path to the files are:
c:\inetpub\wwwroot\hcuk\images\photos\$id.jpg
And the name of the pc is MATTLAPTOP. Can you point me to what the path should be? I assume I will have to change it when I upload to my webhost, but I'd still like to understand where I'm going wrong!
Incidentally the scripts from my earlier post are in the following file:
c:\inetpub\wwwroot\hcuk\squad.php
This is why I thought the relative path should work?
Posted: Tue Jun 14, 2005 4:29 am
by JayBird
change this line
Code: Select all
$filename = "/images/photos/$id.jpg";
to...
Code: Select all
$filename = $_SERVER['DOCUMENT_ROOT']."/images/photos/$id.jpg";
Posted: Tue Jun 14, 2005 8:33 am
by frolicols
Pimptastic wrote:change this line
Code: Select all
$filename = "/images/photos/$id.jpg";
to...
Code: Select all
$filename = $_SERVER['DOCUMENT_ROOT']."/images/photos/$id.jpg";
Worked like a charm, thanks!