Jump to content

file_exists problems


jonlivesley

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/187349-file_exists-problems/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-993246
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994138
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994145
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/187349-file_exists-problems/#findComment-994161
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.