简体   繁体   中英

why file_exists cannot find file?

I have a PHP script located at /var/www/html/index.php and need to open /var/log/apache2/access.log .

Through the console, the file exists and in the browser the other way around.

Here is my code :

$filename = '/var/log/apache2/error.log';
if (file_exists($filename)) {
    exit("Файл $filename существует");
} else {
    exit("Файл $filename не существует");
}

Warning: This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

from php-manual .

Perhaps set the path wrong, try

var_dump($_SERVER["DOCUMENT_ROOT"]."/myFolder/*");

google for $_SERVER path coms.

Or similar.

us error_reporting(E_ALL) at the top of the code to see what path it throws back when it doesnt find it.

and like zane pointed it out, it most likely cannot reach root files, its usually restricted area.

You can add echo exec("whoami") . "\\n"; echo exec("whoami") . "\\n"; to your script to know who is the role used by the HTTP server (usually www-data with apache) and configure the permissions on your server.

You can also configure an other path for your logs using the CustomLog and ErrorLog directive in the configuration of your web-server or in the .htaccess file

example:

CustomLog /var/www/html/logs/access.log combined
ErrorLog /var/www/html/logs/error.log

Documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM