jonlivesley Posted January 6, 2010 Share Posted January 6, 2010 Hey. I have an issue with the file_exists function not working as expected and I haven't been able to crack it anyhow despite reading many supposed solutions. Basically I maintain the site http://www.thebasementyork.co.uk which is PHP driven and based on a MySQL database. Some of the pages are generated including an image file the (relative) pathname of which is stored as one of the fields in the database. This all works fine and the pages display as they should. However, I've been building a backend to help me spot problems occuring in the database. One of these test to see if the pathname points to a file that exists, as such: $result = mysql_query( "SELECT EventID, EventName, EventStart, PosterURL FROM eventstable ORDER BY EventStart ASC" ); while ( $row = mysql_fetch_array( $result ) ) { if ( !file_exists( $_SERVER['DOCUMENT_ROOT'].$row['PosterURL'] ) && isset( $row['PosterURL'] ) ) { /* echo details of the entry that needs correcting */ } } This works fine for all files in the posters folder apart from those in a subfolder that is used to store posters that are used for multiple events (due to the file naming rules I use). This is for events such as this one (http://www.thebasementyork.co.uk/event/29/) - which displays the poster even though PHP is telling me it doesn't exist. It doesn't seem to be a permissions issue as I've granted maximum permissions for the subfolder. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/ Share on other sites More sharing options...
jonlivesley Posted January 12, 2010 Author Share Posted January 12, 2010 Does anyone have any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-993240 Share on other sites More sharing options...
PFMaBiSmAd Posted January 12, 2010 Share Posted January 12, 2010 How about using var_dump() on both $_SERVER['DOCUMENT_ROOT'] and $row['PosterURL'] so that you can tell if they contain something unexpected. You are testing values in your code and the test is failing. Investigate what exactly is being tested. Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-993246 Share on other sites More sharing options...
jonlivesley Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks for the suggestion, but all var_dump returns is: string(47) "/event-posters/regulars/takethestageposter.jpg " string(26) "/home/thebasem/public_html" Which is what I would have expected so doesn't really help. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994138 Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2010 Share Posted January 13, 2010 If you actually look that the results (and count the characters) you will notice that there is probably a space on the end of the first value that will make the file_exists test fail. Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994143 Share on other sites More sharing options...
oni-kun Posted January 13, 2010 Share Posted January 13, 2010 Thanks for the suggestion, but all var_dump returns is: string(47) "/event-posters/regulars/takethestageposter.jpg " string(26) "/home/thebasem/public_html" Which is what I would have expected so doesn't really help. Any other ideas? Are you 100% sure they fit together? $_SERVER['DOCUMENT_ROOT'].$row['PosterURL'] Try checking the result of: echo= '/home/thebasem/public_html' . $row['PosterURL']; It may be possible the path is not completely correct, such as a missing forward slash. This would cause file_exist to return a false, as you could imagine. Only thing I can think of right now. Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994145 Share on other sites More sharing options...
salathe Posted January 13, 2010 Share Posted January 13, 2010 string(47) "/event-posters/regulars/takethestageposter.jpg " As PFMaBiSmAd said, there is a space character at the end of this string which (I'll make an educated guess here) should not be there. Change the value in the database to not have that space and that should be your issue solved. It's probably worth making sure no extra spaces are appended/prepended to paths when inserting them into the database initially. Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994161 Share on other sites More sharing options...
jonlivesley Posted January 13, 2010 Author Share Posted January 13, 2010 Cheers guys. It seems the unneccessary space was what was causing the problem. Pretty sloppy work by me! Have added a trim function to hopefully make sure this doesn't happen again. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.