0

I'm trying to get all files from a main directory and from all its subdirectories with this PHP code:

function allFiles($dir) {
    $files = [];
    foreach(glob($dir.'*') as $fileOrDir) {
        if(is_dir($fileOrDir)) {
            $files = array_merge($files, allFiles($fileOrDir));
        } else {
            $files[] = $fileOrDir; 
        }
    }
    return $files;
}

$allFiles = allFiles($_SERVER['DOCUMENT_ROOT'].'/contents/');
foreach($allFiles as $file) {
    echo $file.'<br>';
}

But I get the error 500 Internal Server Error.

As I see a problem is in the line

$files = array_merge($files, allFiles($fileOrDir));

because the code without this line works without any errors

But what's the problem?

stckvrw
  • 1,689
  • 18
  • 42
  • What does the web server error log say about it? – Dave Apr 24 '18 at 18:14
  • I'm on shared hosting with cPanel. Where can I see error log file ? – stckvrw Apr 24 '18 at 18:23
  • 1
    Look for Errors or Error Log in cPanel. If your host is using Apache you should see it there. – Dave Apr 24 '18 at 18:24
  • https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – mkasberg Apr 24 '18 at 18:26
  • Does it require the root access? Because I don't have such access – stckvrw Apr 24 '18 at 18:28
  • It probably requires root to view `/usr/local/apache/logs/error_log`. But if you don't have root access, it is also likely that your log is configured at a different location where you do have access. – mkasberg Apr 24 '18 at 18:32
  • I have the `/logs` directory and it contains some .gz files, incuding my subdomain. But when I download and unpack the file of my subdomain, I don't see records for the last hours – stckvrw Apr 24 '18 at 18:38
  • With `phpinfo()` I see that the `error_log` directive has local and master values `error_log` but the `log_errors` directive has values `Off` – stckvrw Apr 24 '18 at 18:43
  • @Quentin it's not a problem with displaying an error, but with specific PHP code – stckvrw Apr 24 '18 at 18:53
  • 1
    @stckvrw — Specific PHP code which is causing an unknown error. Find out the error. Then you can find out how to fix it. – Quentin Apr 24 '18 at 18:56
  • No, you're wrong. It's enough to look closer to the code to find out the problem. The line with `array_merge()` has no slash at the end of the `allFiles()` function argument, it was all the problem. – stckvrw Apr 24 '18 at 22:20

1 Answers1

-1

500 Internal Server Error is a generic exception that is returned by PHP to the web browser when an uncaught exception occurs. In order to debug the problem, you need to get information about the more specific exception that is being thrown. The configuration you're currently using is common in production scenarios where the details of internal exception messages should not be exposed to the public, but it can make debugging a little harder.

Here are some things you can try to get the real exception message:

  • Look at the log files. The location varies by configuration, but here are some places to check:
    • /var/log/apache2
    • /var/log/httpd
    • /usr/local/apache/logs/error_log
    • /var/log/messages
    • Look at the output of phpinfo() and grep for error_log.
    • Look at the contents of ls /var/log and see if there is another obvious location there.
    • Custom file log, or special configuration such as an external log search utility like Graylog.
  • Use a debugger. Step through the problematic part of code, and watch for exceptions.
  • Turn on error display, but be careful if this is a production environment.
    • Set error_reporting to E_ALL in php.ini.
    • Set display_errors to 1 in php.ini.
mkasberg
  • 16,022
  • 3
  • 42
  • 46