koopkoop Posted May 6, 2008 Share Posted May 6, 2008 Trying to find out if an image in a directory /images/products/a.jpg exists. I know the file exists, but my code always returns a FALSE. Experimenting shows that any file in the same folder as my test script can be found. However, as soon as i move to a file in a differnt folder, it always returns false. What am I doing wrong? //This returns true echo file_exists("index.php"); //This returns false echo file_exists("/Images/Products/11003-L.jpg"); Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/ Share on other sites More sharing options...
realjumper Posted May 6, 2008 Share Posted May 6, 2008 You said: "Trying to find out if an image in a directory /images/products/a.jpg exists." <-- note the lower case directory names Your code says: "echo file_exists("/Images/Products/11003-L.jpg");" <-- note the caps in the directory names Not sure if the function is case sensitive or not.....change it and see Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534138 Share on other sites More sharing options...
trq Posted May 6, 2008 Share Posted May 6, 2008 Also, is the images directory really in the root of your filesystem? Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534141 Share on other sites More sharing options...
koopkoop Posted May 6, 2008 Author Share Posted May 6, 2008 I'll need to check to see if the case sensitivity is a factor or not. Yes, I know for sure the file paths are correct. I use the exact same path to actually display the image and the images display properly. Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534155 Share on other sites More sharing options...
koopkoop Posted May 6, 2008 Author Share Posted May 6, 2008 I changed the case of my files/folders to amtch the script and still no luck. I found this in the file_exists() comment section on the official PHP site: "If the file being tested by file_exists() is a file on a symbolically-linked directory structure, the results depend on the permissions of the directory tree node underneath the linked tree. PHP under a web server (i.e. apache) will respect permissions of the file system underneath the symbolic link, contrasting with PHP as a shell script which respects permissions of the directories that are linked (i.e. on top, and visible). This results in files that appear to NOT exist on a symbolic link, even though they are very much in existance and indeed are readable by the web server." AND "file_exists under safe_mode works (I work with for php 4) doesn't work when owner of the dir where the file stands differs from the php-owner. e.g. file_exists in this dir will give FALSE nevertheless the file exists rights owner dir drwxr-xr-x apache images change the owner of the dir to the phpowner and file_exists works. I spent too much time to discover. Hope it will spare a lot of your time. Stefan" I have no idea what that means. Can someone explain what it means and how I would check that? Also, has anyone successfully used file_exists. What's your code and what's your folder structure look like? Thanks! Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534158 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2008 Share Posted May 6, 2008 A leading slash / in a file system path refers to the root of the current disk, which is what thorpe ask if you wanted your existing code to be doing. This is not the same as a leading slash / in a relative URL, which refers to the domain root. If the images folder is in your domain root, I recommend forming an absolute file system path as follows - $_SERVER['DOCUMENT_ROOT'] . "/path_to_file/file" $_SERVER['DOCUMENT_ROOT'] . "/images/products/11003-L.jpg" echo file_exists($_SERVER['DOCUMENT_ROOT'] . "/images/products/11003-L.jpg"); Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534249 Share on other sites More sharing options...
koopkoop Posted May 6, 2008 Author Share Posted May 6, 2008 A leading slash / in a file system path refers to the root of the current disk, which is what thorpe ask if you wanted your existing code to be doing. This is not the same as a leading slash / in a relative URL, which refers to the domain root. If the images folder is in your domain root, I recommend forming an absolute file system path as follows - $_SERVER['DOCUMENT_ROOT'] . "/path_to_file/file" $_SERVER['DOCUMENT_ROOT'] . "/images/products/11003-L.jpg" echo file_exists($_SERVER['DOCUMENT_ROOT'] . "/images/products/11003-L.jpg"); Thank you, so much! That was exactly it. It works now! Now some more questions (Please correct me if my wording is off, I'm totally new to this): 1)So anytime I'm working with straight up HTML, I only need to care about URL formatting? I'm not even sure if it's possible for HTML to deal directly with the filesystem or if HTML is even aware of the server filesystem - I just want to make sure I'm clear on this. 2)Anytime I'm referencing any files in PHP, I need to consider it from the filesystem perspective and not the HTML perspective? So, this means that all of the "require()"s I'm thinking of adding in later should be done with the same $_server['document_root'] method? 3)Now as an extension on the train of thought in questions 1 and 2, sinceI'm starting to wonder....is PHP even aware of HTML conventions? It isn't is it? It basically spits out (echos) HTML code, but isn't even "aware" of it?! Therefore, anytime I'm working with PHP - and not using it to echo HTML code - I'll always only need to worry about the filesystem? If so, this makes things a lot easier for me to understand - so I hope this is true hahaha. Is it? Again, thank you so much for the help. I spent an hour looking for an answer to my original question and couldn't find the silver bullet that you ended up providing. Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534470 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2008 Share Posted May 6, 2008 1) Browsers make http/https requests to web servers. So, a browser only deals with URL's and they can only request files that can be reached through a URL. Browsers form absolute URL's out of relative URL's (those starting with just a path/name or a ./ or a ../) by taking the address of the current page and appending any relative URL. The leading slash / relative URL results in the browser taking the domain of the current page and appending the relative URL. 2) By default php functions assume a file system reference. If it is enabled, you can use a URL (http://some_domain/some_path/some_file), in which case php makes a http/https request to the URL you entered (even if it is on the same server with the script making the request.) If the http/https request is for a .php file, that file will be parsed as a separate web server action and only the content that is output by the file will be received. 3) Php is a programming language and the output it produces in only dependent on the logic you write. If you write code that outputs valid HTML (or CSS or Javascript), you get valid html/CSS/Javascript. If you write code that outputs invalid HTML, you get invalid html. Link to comment https://forums.phpfreaks.com/topic/104325-file_exists-not-working/#findComment-534652 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.