I'm trying to create a script for including (through require_once) multiple files, but I'm expecting from it following behavior:
- all file names of required files are defined as values in array
- script check if all files from array exist in given directory
- if yes, require them and continue (only if each of them exist)
- if no, terminate script and show error message (if any file is missing)
This is what I got so far but it doesn't work as it should even after adding error check (existing files are still included even if there were errors):
// Array with required file names
$files = array('some_file', 'other_file', 'another_file');
// Count how many files is in the array
$count = count($files);
// Eampty array to catch errors
$errors = array();
for ($i=0; $i < $count; $i++) {
// If filename is in the array but file not exist in directory...
if (in_array($files[$i], $files) && !file_exists(ROOT_DIR . $files[$i] . '.php')) {
// ...add name of missing file to error array
$errors[] = $files[$i];
}
// Count errors
$countErr = count($errors);
// If there was no errors...
if ($countErr == 0) {
foreach ($files as $file) {
// ...include all files
require_once (ROOT_DIR . $file . '.php');
}
} else { // If there were errors...
foreach ($errors as $error) {
// ...show error message with missing filenames
echo "File: " . ROOT_DIR . $error . ".php wasn't found.";
}
}
}
I'll be grateful for help with finding right solution for this problem.
Aucun commentaire:
Enregistrer un commentaire