user1844626
user1844626

Reputation: 1858

How can check a php file is successfully included?

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?

Upvotes: 11

Views: 24405

Answers (5)

Cerbrus
Cerbrus

Reputation: 72839

Simply use require:

try {
    require 'filename.php';
} catch (Error $e) {
    // Handle $e some other way instead of `exit`-ing, if you wish.
}

You can catch this error only as of PHP 8.0.

Something that wasn't mentioned yet: you could add a boolean, like:

$dbasIncluded = true;

In your dbas.php file, then check for that boolean in your code. Although generally, if a file doesn't include properly, you'd want php to hit the brakes, instead of rendering the rest of the page.

Upvotes: 8

The Mechanic
The Mechanic

Reputation: 2329

Use this code instead of your code, because in your code if file is not exist in server then php errors arise and that is not good so use this code:

if(file_exists("dbas.php")) {
    include_once "dbas.php";
} else {
    echo "file is not found";
}

This code means if file exists on server then function include else file is not found echo.

Upvotes: 0

Bart Friederichs
Bart Friederichs

Reputation: 33491

Put your functionality in a function and use function_exists to check if it is there.

include "php_file_with_fcn.php";
if (function_exists("myFunc")) {
    myFunc();
    // run code
} else {
    echo "failed to load";
}

In your case, the incusion file would be

function db_connect() {
     $user = "user";
     $pass = "pass";
     $host = "host";
     $database = "database";
     mysql_connect($host, $user, $pass);
     return mysql_select_db($database);
}

and the main file:

include "db_connect.php";
if (function_exists("db_connect")) {
    if (db_connect() === TRUE) {
        // continue
     } else {
        // failed to connect (this is a different error than the "can't include" one and 
        // actually **way** more important to handle gracefully under great load
     }
 } else {
     // couldn't load database code
 }

Upvotes: 0

Alasjo
Alasjo

Reputation: 1240

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 */
}

Upvotes: 17

Paul Dessert
Paul Dessert

Reputation: 6389

file_exists("dbas.php") Is doing the checking. If it exists, then do the include.

if(file_exists("dbas.php"){
    include("dbas.php")
    //continue with you code here
}

Upvotes: 0

Related Questions