Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please don't laugh

Status
Not open for further replies.

daybase

Technical User
Joined
Dec 13, 2002
Messages
115
Location
GB
Until the early hours of the morning I have been trying to test for the existence of a file using file_exists(). If I hard code the file name it works fine but if I use a variable from a table it doesn't. Am I just stupid or what? Please help so I can get some sleep - snippet of code to demonstrate:


$filename=$row["phref"]

#note phref is Dscf0011.jpg

if (file_exists('thumb/Dscf00011.jpg')) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

#The above works

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

#The above doesn't
 
The only difference between the first and the second snippet is that the second has no path information.

You say phref is Dscf0011.jpg. The function will not work the same unless you test for the path+filename:
Code:
if (file_exists([COLOR=red]'thumb/'.[/color]$filename)) {
   echo "The file $filename exists";
} else {
   echo "The file $filename does not exist";
}

I'm assuming the value in $row['phref'] is correct - check that with a print statement. But as it looks, it might be the path.
 
I had actually included the path name in the file name but i've simply copied and pasted your code and all is well for no apparent reason. Thanks a lot.
 
Am I being dim? if_exists() works fine if the path is entered into the script but if I try to construct if from a variable it doesn't. For example I'm certin that the variable $fx produces exaxctly the same string as the hard coded string but the if_exists using the variable gives the wrong result but the other works fine! Where am I going wrong PLEASE

$Pthumb="images/";
#$Pthumb = pathname images/
$spix="dog.jpg";
#$spix = file name dog.jpg


$tmp=$Pthumb.$spix;
$fx="file_exists('$tmp')";

# at this stage i reckon $fx= file_exists('images/dog.jpg')

if($fx){
echo"file DOES exist";
}else{
echo"file DOES NOT exist";
}

echo"<p>";

if(file_exists('images/dog.jpg')){
echo"file DOES exist";
}else{
echo"file DOES NOT exist";
}


 
Code:
$tmp=$Pthumb.$spix;
#[COLOR=red]you just stored the PHP code in the variable fx[/color]
$fx="file_exists('$tmp')";

# at this stage i reckon $fx= file_exists('images/dog.jpg')

if($fx){
You expect that $fx is just evaluated by using if($fx). It is nothing but a string. you would have to use the eval() function to actually evaluate the expression. That's what's going on.
 
very new to all this so please excuse the stupid questions. Am I going about this the wrong way ? - all I am after doing is checking for the existence of a file variable name $spix with a path variable name $Pthumb.

My reasoning says if(file_exists($Pthumb.$spix)){
echo etc etc etc

should work but it doesn't.
 
No, you are doing it right. Have you removed the double quotes around the expression that assigns $fx?
If not, $fx will always be true, since $fx = "whatever" returns true.
Try the following code:
Code:
$Pthumb="images/";
$spix="dog.jpg";
$tmp=$Pthumb.$spix;

#debug
echo("Value is: ".$tmp."<br />");
if(file_exists($tmp)){
    echo"file DOES exist";
}else{
    echo"file DOES NOT exist";
}
 
My problem appears to be to do with the path element - can file_exists check a file in an unrelated directory? I am holding common data in a directory from where various web sites will be extracting images so this if_exists page will be used in a number of locations. So in the above example $Pthumb variable would be

$Pthumb="
But it doesn't work. Sorry you got involved?
 
All file functions should be used with the server specific path as the filesystem has it.
These functions do not take the web server document root into consideration. You can read anywhere in the file system, but you have to start at the root. So, if you document root is
/var/ you need to prepend that to path.
 
FANTASTIC! thank you so much - would never have got that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top