file_exists() problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frolicols
Forum Newbie
Posts: 7
Joined: Mon Jun 13, 2005 10:25 am
Location: Hertford, UK

file_exists() problem

Post 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!! :cry:
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post 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 '/'
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
frolicols
Forum Newbie
Posts: 7
Joined: Mon Jun 13, 2005 10:25 am
Location: Hertford, UK

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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";
frolicols
Forum Newbie
Posts: 7
Joined: Mon Jun 13, 2005 10:25 am
Location: Hertford, UK

Post 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!
Post Reply