I want to check if 'dbas.php' is included in 'ad.php'. I wrote the code:
<?php if(file_exists("dbas.php") && include "dbas.php"){
// some code will be here
}
else{echo"Database loading failed";}
?>
I successfully tested the file_exists() part but don't know if the include() will work well or not, cause I tried in localhost and if the file is in directory then it never fails to include. So I don't know how this code would behave in the server if much traffic be there. So please tell me is my code correct?
Using php's require
method is more suitable if you want to be absolutely sure that the file is included. file_exists
only checks if the file exists, not if it's actually readable.
require
will produce an error if inclusion fails (you can catch
the error, see Cerbrus' answer).
However, if you don't want the script to halt if the inclusion fails, use the method is_readable
along with file_exists
, like:
if( file_exists("dbas.php") && is_readable("dbas.php") && include "dbas.php") {
/* do stuff */
}