I am new to PHP and am trying to construct a page that will select files from a directory either manually or based on a time period in which the file was created.

First of all, does anyone know of where I can get code that will do that?

Second of all, I am writing a script but in the script I am getting a weird error.

Here is the snippet of the code that I am having trouble with:

if (isset($_POST['command']))
    echo 'Command: ' . $_POST['command'] . '<br>';
    $exit = cmd_exec($_POST['command'],$stdout, $stderr);

    //print the output
    foreach ($stdout as $line)
        echo "$line<br>";

    //in case there an error is returned
    foreach ($stderr as $line)
        echo "$line<br>";
        $dir = "/tmp";
        if($h = opendir($dir)) {
          $files = array();
          while(($file = readdir($h) !== FALSE)
            $files[] = stat($file);

        // do the sort
          usort($files, 'your_sorting_function');

        // do something with the files
          foreach($files as $file) {
            echo htmlspecialchars($file);


The error I am getting is:

PHP Parse error:  syntax error, unexpected T_VARIABLE in /var/www/html/run.php on line 61

Line 61 is $files[] = stat($file);

I first thought I was missing { } brackets in the while but that did not seem to help, only changed the error to:

syntax error, unexpected '{' in /var/www/html/run.php on line 61




Clarification - I need to select files on the server not on the Client computer.

I then need to use this file list as input to another command executed on each file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.