This warning message is due to server configuration settings.open_basedirThe restriction limits the file system access scope of the PHP script; since your script is attempting to access a directory outside this permitted range, a warning is raised.
There are several ways to solve this problem:
- Modify server configuration
You can contact the server administrator to request modifications to the server.open_basedirFor configuration purposes, add the directory you wish to access to the allowed list to resolve this issue.
- Use other functions
scandir()The function is affected.open_basedirThe impact of restrictions; however, PHP provides other functions that can be used to retrieve files and subdirectories from a directory, such asglob()和readdir()Functions. You can try using these functions as an alternative.scandir()A function to solve this problem.
- Skip restricted directory
You can incorporate exception handling into your code; whenscandir()When the function throws a warning, it catches the exception and skips accessing directories with restricted access. The example code is as follows:
function getDirectories($dir) {
$directories = array();
if (is_dir($dir)) {
try {
$files = scandir($dir);
foreach ($files as $file) {
if ($file != "." && $file != ".." && is_dir($dir . "/" . $file)) {
$directories[] = $file;
}
}
} catch (Exception $e) {
// 忽略访问受限目录时的警告
}
}
return $directories;
}In this example code, we usetry-catchStructure for capturingscandir()Any exception thrown by the function – if an exception occurs (e.g., when attempting to access a restricted directory) – will cause the function to skip that operation directly. This allows us to ignore the warning when accessing a restricted directory without affecting the normal execution of the program.
Please note that this method may cause certain directories to be missed, as restricted directories are not scanned. Therefore, this approach is only suitable for less critical directories and is not recommended for scenarios where all directories must be scanned.
After all that, let me briefly share my solution!
BaoTa Panel Solution
Delete the website's root directory..user.iniThis document
https://www.4s5.cn/archives/1210.html