I want to check if ‘dbas.php’ is included in ‘ad.php’. I wrote the code –
ad.php
<?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 ?
-Thanks.
Solved: Thank you so much for your answers.
Using php’s
requiremethod is more suitable if you want to be absolutely sure that the file is included.file_existsonly checks if the file exists, not if it’s actually readable.requirewill produce an error if inclusion fails (you cancatchthe error, see Cerbrus’ answer).Edit:
However, if you don’t want the script to halt if the inclusion fails, use the method
is_readablealong withfile_exists, like: