diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index 7f31aef04cd9..4735119c85f9 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -326,7 +326,7 @@ protected function requireFile(string $file) { $file = $this->sanitizeFilename($file); - if (file_exists($file)) + if (is_file($file)) { require_once $file; diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index f846e4a68efe..94bef703f1a0 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -233,7 +233,7 @@ public function search(string $path, string $ext = 'php'): array { $folder = rtrim($folder, '/') . '/'; - if (file_exists($folder . $path)) + if (is_file($folder . $path) === true) { $foundPaths[] = $folder . $path; } @@ -369,7 +369,7 @@ protected function legacyLocate(string $file, string $folder = null): string */ protected function requireFile(string $path): bool { - return file_exists($path); + return is_file($path); } //-------------------------------------------------------------------- diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index 3a94238e9940..3a14e5e0a6f2 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -137,7 +137,7 @@ public function delete(string $key) { $key = $this->prefix . $key; - return file_exists($this->path . $key) ? unlink($this->path . $key) : false; + return is_file($this->path . $key) ? unlink($this->path . $key) : false; } //-------------------------------------------------------------------- @@ -246,7 +246,7 @@ public function getMetaData(string $key) { $key = $this->prefix . $key; - if (! file_exists($this->path . $key)) + if (! is_file($this->path . $key)) { return false; } @@ -473,7 +473,7 @@ protected function getDirFileInfo($source_dir, $top_level_only = true, $_recursi */ protected function getFileInfo(string $file, array $returned_values = ['name', 'server_path', 'size', 'date']) { - if (! file_exists($file)) + if (! is_file($file)) { return false; } diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 91dab4cd53e4..5043b345a9f9 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -411,7 +411,7 @@ protected function detectEnvironment() */ protected function bootstrapEnvironment() { - if (file_exists(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) + if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; } diff --git a/system/ComposerScripts.php b/system/ComposerScripts.php index 377ccfd56152..87bbc2e2f4c8 100644 --- a/system/ComposerScripts.php +++ b/system/ComposerScripts.php @@ -142,7 +142,7 @@ protected static function removeDir($dir) */ public static function moveEscaper() { - if (class_exists('\\Zend\\Escaper\\Escaper') && file_exists(static::getClassFilePath('\\Zend\\Escaper\\Escaper'))) + if (class_exists('\\Zend\\Escaper\\Escaper') && is_file(static::getClassFilePath('\\Zend\\Escaper\\Escaper'))) { $base = static::$basePath . 'ZendEscaper'; @@ -181,7 +181,7 @@ public static function moveKint() { $filename = 'vendor/kint-php/kint/build/kint-aante-light.php'; - if (file_exists($filename)) + if (is_file($filename)) { $base = static::$basePath . 'Kint'; diff --git a/system/Database/SQLite3/Forge.php b/system/Database/SQLite3/Forge.php index c68e75c12d41..a6f9a58bb2e1 100644 --- a/system/Database/SQLite3/Forge.php +++ b/system/Database/SQLite3/Forge.php @@ -103,7 +103,7 @@ public function createDatabase($db_name): bool public function dropDatabase($db_name): bool { // In SQLite, a database is dropped when we delete a file - if (! file_exists($db_name)) + if (! is_file($db_name)) { if ($this->db->DBDebug) { diff --git a/system/Debug/Toolbar.php b/system/Debug/Toolbar.php index d796d29bf222..041e860ebafe 100644 --- a/system/Debug/Toolbar.php +++ b/system/Debug/Toolbar.php @@ -458,7 +458,7 @@ public static function eventHandler() $filename = WRITEPATH . 'debugbar/' . $file; // Show the toolbar - if (file_exists($filename)) + if (is_file($filename)) { $contents = static::format(file_get_contents($filename), $format); exit($contents); diff --git a/system/Email/Email.php b/system/Email/Email.php index 0ca3c24e42b9..87f5c82e3aa8 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -734,7 +734,7 @@ public function attach($file, $disposition = '', $newname = null, $mime = '') { if ($mime === '') { - if (strpos($file, '://') === false && ! file_exists($file)) + if (strpos($file, '://') === false && ! is_file($file)) { $this->setErrorMessage(lang('Email.attachmentMissing', [$file])); diff --git a/system/Events/Events.php b/system/Events/Events.php index 41311f1729a5..f22b658a149f 100644 --- a/system/Events/Events.php +++ b/system/Events/Events.php @@ -112,7 +112,7 @@ public static function initialize() foreach (static::$files as $file) { - if (! file_exists($file)) + if (! is_file($file)) { continue; } diff --git a/system/Files/File.php b/system/Files/File.php index 195d2ce2bb04..5996e4ce5215 100644 --- a/system/Files/File.php +++ b/system/Files/File.php @@ -199,7 +199,7 @@ public function move(string $targetPath, string $name = null, bool $overwrite = */ public function getDestination(string $destination, string $delimiter = '_', int $i = 0): string { - while (file_exists($destination)) + while (is_file($destination)) { $info = pathinfo($destination); if (strpos($info['filename'], $delimiter) !== false) diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 6cbbc17459ef..41af34ec4def 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -581,7 +581,7 @@ protected function setCURLOptions(array $curl_options = [], array $config = []) $cert = $cert[0]; } - if (! file_exists($cert)) + if (! is_file($cert)) { throw HTTPException::forSSLCertNotFound($cert); } diff --git a/system/HTTP/Files/UploadedFile.php b/system/HTTP/Files/UploadedFile.php index 70419f888330..98052ebec44f 100644 --- a/system/HTTP/Files/UploadedFile.php +++ b/system/HTTP/Files/UploadedFile.php @@ -204,7 +204,7 @@ protected function setPath($path) { mkdir($path, 0777, true); //create the index.html file - if (! file_exists($path . 'index.html')) + if (! is_file($path . 'index.html')) { $file = fopen($path . 'index.html', 'x+'); fclose($file); diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index c4389b4eb129..dd1825e44925 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -331,7 +331,7 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool */ function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']) { - if (! file_exists($file)) + if (! is_file($file)) { return null; } diff --git a/system/Log/Handlers/FileHandler.php b/system/Log/Handlers/FileHandler.php index 8b2b43837749..12ad9ea8bf32 100644 --- a/system/Log/Handlers/FileHandler.php +++ b/system/Log/Handlers/FileHandler.php @@ -101,7 +101,7 @@ public function handle($level, $message): bool $msg = ''; - if (! file_exists($filepath)) + if (! is_file($filepath)) { $newfile = true; diff --git a/system/Router/Router.php b/system/Router/Router.php index e10f3f27bad4..3c7a22efd77d 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -530,7 +530,7 @@ public function autoRoute(string $uri) // Load the file so that it's available for CodeIgniter. $file = APPPATH . 'Controllers/' . $this->directory . $this->controller . '.php'; - if (file_exists($file)) + if (is_file($file)) { include_once $file; } @@ -564,7 +564,7 @@ protected function validateRequest(array $segments) $test = $this->directory . ucfirst($this->translateURIDashes === true ? str_replace('-', '_', $segments[0]) : $segments[0] ); - if (! file_exists(APPPATH . 'Controllers/' . $test . '.php') && $directory_override === false && is_dir(APPPATH . 'Controllers/' . $this->directory . ucfirst($segments[0])) + if (! is_file(APPPATH . 'Controllers/' . $test . '.php') && $directory_override === false && is_dir(APPPATH . 'Controllers/' . $this->directory . ucfirst($segments[0])) ) { $this->setDirectory(array_shift($segments), true); @@ -663,7 +663,7 @@ protected function setDefaultController() $this->method = 'index'; } - if (! file_exists(APPPATH . 'Controllers/' . $this->directory . ucfirst($class) . '.php')) + if (! is_file(APPPATH . 'Controllers/' . $this->directory . ucfirst($class) . '.php')) { return; } diff --git a/system/Session/Handlers/FileHandler.php b/system/Session/Handlers/FileHandler.php index d6531a46bf88..1c4cb720b60e 100644 --- a/system/Session/Handlers/FileHandler.php +++ b/system/Session/Handlers/FileHandler.php @@ -153,7 +153,7 @@ public function read($sessionID) // which re-reads session data if ($this->fileHandle === null) { - $this->fileNew = ! file_exists($this->filePath . $sessionID); + $this->fileNew = ! is_file($this->filePath . $sessionID); if (($this->fileHandle = fopen($this->filePath . $sessionID, 'c+b')) === false) { @@ -302,13 +302,13 @@ public function destroy($session_id): bool { if ($this->close()) { - return file_exists($this->filePath . $session_id) ? (unlink($this->filePath . $session_id) && $this->destroyCookie()) : true; + return is_file($this->filePath . $session_id) ? (unlink($this->filePath . $session_id) && $this->destroyCookie()) : true; } elseif ($this->filePath !== null) { clearstatcache(); - return file_exists($this->filePath . $session_id) ? (unlink($this->filePath . $session_id) && $this->destroyCookie()) : true; + return is_file($this->filePath . $session_id) ? (unlink($this->filePath . $session_id) && $this->destroyCookie()) : true; } return false; diff --git a/system/View/Parser.php b/system/View/Parser.php index 44c81488307f..37a992d4e493 100644 --- a/system/View/Parser.php +++ b/system/View/Parser.php @@ -143,7 +143,7 @@ public function render(string $view, array $options = null, $saveData = null): s $view = $view . '.php'; $file = $this->viewPath . $view; - if (! file_exists($file)) + if (! is_file($file)) { $file = $this->loader->locateFile($view, 'Views'); } diff --git a/system/View/View.php b/system/View/View.php index 5e810ecb9178..7df71925957a 100644 --- a/system/View/View.php +++ b/system/View/View.php @@ -187,7 +187,7 @@ public function render(string $view, array $options = null, $saveData = null): s $this->renderVars['file'] = $this->viewPath . $this->renderVars['view']; - if (! file_exists($this->renderVars['file'])) + if (! is_file($this->renderVars['file'])) { $this->renderVars['file'] = $this->loader->locateFile($this->renderVars['view'], 'Views', empty($fileExt) ? 'php' : $fileExt); } diff --git a/system/bootstrap.php b/system/bootstrap.php index 1121a07bc38b..30a1d3320f73 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -125,7 +125,7 @@ class_alias('Config\Services', 'CodeIgniter\Services'); $loader->register(); // Register the loader with the SPL autoloader stack. // Now load Composer's if it's available -if (file_exists(COMPOSER_PATH)) +if (is_file(COMPOSER_PATH)) { require_once COMPOSER_PATH; } diff --git a/tests/system/Cache/Handlers/FileHandlerTest.php b/tests/system/Cache/Handlers/FileHandlerTest.php index abc751f3dc8f..838ba5378fed 100644 --- a/tests/system/Cache/Handlers/FileHandlerTest.php +++ b/tests/system/Cache/Handlers/FileHandlerTest.php @@ -187,7 +187,10 @@ public function __construct() public function getFileInfoTest() { - return $this->getFileInfo($this->config->storePath, [ + $tmp_handle = tmpfile(); + stream_get_meta_data($tmp_handle)['uri']; + + return $this->getFileInfo(stream_get_meta_data($tmp_handle)['uri'], [ 'name', 'server_path', 'size', diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 300200bc30f0..8ceaffeb2c9a 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -67,7 +67,7 @@ Classmap ======== The classmap is used extensively by CodeIgniter to eke the last ounces of performance out of the system -by not hitting the file-system with extra ``file_exists()`` calls. You can use the classmap to link to +by not hitting the file-system with extra ``is_file()`` calls. You can use the classmap to link to third-party libraries that are not namespaced:: $classmap = [ diff --git a/user_guide_src/source/tutorial/static_pages.rst b/user_guide_src/source/tutorial/static_pages.rst index c5d664462236..c8a408eaea2f 100644 --- a/user_guide_src/source/tutorial/static_pages.rst +++ b/user_guide_src/source/tutorial/static_pages.rst @@ -98,7 +98,7 @@ page actually exists: public function view($page = 'home') { - if ( ! file_exists(APPPATH.'/Views/pages/'.$page.'.php')) + if ( ! is_file(APPPATH.'/Views/pages/'.$page.'.php')) { // Whoops, we don't have a page for that! throw new \CodeIgniter\Exceptions\PageNotFoundException($page); @@ -116,7 +116,7 @@ footer, and displayed to the user. If the page doesn't exist, a "404 Page not found" error is shown. The first line in this method checks whether the page actually exists. -PHP's native ``file_exists()`` function is used to check whether the file +PHP's native ``is_file()`` function is used to check whether the file is where it's expected to be. The ``PageNotFoundException`` is a CodeIgniter exception that causes the default error page to show.