Jump to content

file_exists() doesn't detect files


ShadowMage

Recommended Posts

I have the following code:

$fName = 'NotFound';if (file_exists('/'.$rootDir.'/images/PanelTypes/Type'.$x.'.jpg')) {// if (file_exists('./images/PanelTypes/Type'.$x.'.jpg')) {// if (file_exists('images/PanelTypes/Type'.$x.'.jpg')) {  $fName = 'Type'.$x;}$html.="<td>\n<input type='radio' name='pnlType".$setID."' id='pnlType".$x."_".$setID."' value='".$x."'".$disableFG." />\n";$html.="<label for='pnlType".$x."_".$setID."'>Type ".$x."</label>\n<img src='images/PanelTypes/".$fName.".jpg' alt='Type ".$x."' /></td>\n";

For whatever reason, the only image that ever shows up is the 'NotFound' image. Am I doing something wrong? The commented if statements are variations that I've tried. And yes, I have verified that the file paths are correct (including the $rootDir and $x variables; $x is incremented throughout a loop) and that files exist (some types do and some do not, for example, Type1.jpg exists but Type3.jpg does not but I always get the NotFound image). Please help. Thanks.

Link to comment
Share on other sites

do you have shell access to your server? I would navigate to the folder you are trying to get into, and try a couple commands (these are Linux based) first echo your file_exists path

echo '/'.$rootDir.'/images/PanelTypes/Type'.$x.'.jpg'

then in the shell

$ pwd  //should give you the absolute path to the directory you are, to compare$ ls -la //gives you a list of all the files in the directory

just compare the output of pwd to echo and maybe there's a casing issue, or some little inconsistency in your $rootDir variable. that's what I would do for starters.

Link to comment
Share on other sites

do you have shell access to your server? I would navigate to the folder you are trying to get into, and try a couple commands (these are Linux based) first echo your file_exists path
echo '/'.$rootDir.'/images/PanelTypes/Type'.$x.'.jpg'

then in the shell

$ pwd  //should give you the absolute path to the directory you are, to compare$ ls -la //gives you a list of all the files in the directory

just compare the output of pwd to echo and maybe there's a casing issue, or some little inconsistency in your $rootDir variable. that's what I would do for starters.

The server is a Windows server, but I do have remote access to it. The echo exactly matches the real path to the files (including case) minus the webroot part of it. Do I need to use the full absolute path including drive letter and webroot? That wouldn't make much sense.EDIT: Well, it does work when I use the full path with drive letter and webroot. I changed nothing else in the path. That seems like very bad design. What happens if the webroot changes? Is there a way to dynamically retrieve the webroot?EDIT2: Found it. $_SERVER['DOCUMENT_ROOT'] seems to point to the webroot. Still, it seems foolish that file_exists would require a full path...
I would say the path is not correct. If the path and filenames are correct, then why would PHP say the file doesn't exist?
Very good question. From everything I can see, though, the paths are correct. Edited by ShadowMage
Link to comment
Share on other sites

You can give it a relative path, it doesn't need to be a full path. One other thing to keep in mind is that Windows uses \ as a directory separator, not /. The constant DIRECTORY_SEPARATOR contains the separator that the current server uses.

Very good question. From everything I can see, though, the paths are correct.
It's a trick question, if the path was correct it would find the file. It doesn't find the file because the path is not correct. Windows machines also do not use a single slash to represent the drive root, they use the drive letter.
Link to comment
Share on other sites

You can give it a relative path, it doesn't need to be a full path. One other thing to keep in mind is that Windows uses \ as a directory separator, not /. The constant DIRECTORY_SEPARATOR contains the separator that the current server uses....... Windows machines also do not use a single slash to represent the drive root, they use the drive letter.
I have always used / in my file paths and in fact I am using / in my current file path which uses the full absolute path including drive letter. I am using / in all of my require calls (which are all relative paths). I also realize that Windows uses a drive letter to refer to the drive root, but I am not trying to access the drive root. I am trying to access the web root which I've always been able to do by starting my file path with a /.
It's a trick question, if the path was correct it would find the file. It doesn't find the file because the path is not correct.
Indeed, the path was not correct as it now works using '../images/PanelTypes/Type'.$x.'.jpg' (yes, that's with / not \)I apparently confused myself into using the wrong relative path. :blink:But that still doesn't explain why my original '/'.$rootDir.'.......' didn't work. Like I said, I've always been able to access the webroot with / before. ($rootDir is referring to my application root, not the webroot in case there was any confusion there.) EDIT: After a quick test, it appears that I am also confused about having used / to access the webroot. I must have been confusing PHP paths with HTML paths (as in the src attribute of an image) because using / to access the webroot does work there (I just tested to confirm).Apparently, I'm much too easily confused... :P Edited by ShadowMage
Link to comment
Share on other sites

I am trying to access the web root which I've always been able to do by starting my file path with a /.
I wouldn't rely on that convention, in a Unix or Linux server a single slash refers to the filesystem root, not the web root. $_SERVER['DOCUMENT_ROOT'] is there if you want to access the web root. If you're printing a URL to the browser, like pointing to an image or Javascript file, then starting with a slash does refer to the web root. That is not the case when you're using PHP to access the filesystem.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...