PHP embedded server, any way to configure it to display directory files?

As you know, with PHP 5.4 there is an available server on the server. However, if you are browsing a directory without an index file, instead, listing all available files / directories (e.g. apache) will give you an error.

Now, as I understand it, this is a design, not some kind of mistake. But maybe someone knows if there is a way to configure it to display the contents of the directory?

+5
source share
3 answers

As Colin mentioned in his comment, the integrated server is for debugging purposes only, so you should expect that it will not have all the features that you expect from a full server.

index.php Apache :

<?php
$dir = substr(dirname($_SERVER['PHP_SELF']),strlen($_SERVER['DOCUMENT_ROOT']));
echo "<h2>Index of ".$dir.":</h2>";
$g = glob("*");
usort($g,function($a,$b) {
    if(is_dir($a) == is_dir($b))
        return strnatcasecmp($a,$b);
    else
        return is_dir($a) ? -1 : 1;
});
echo implode("<br>",array_map(function($a) {return '<a href="'.$a.'">'.$a.'</a>';},$g));
+7

php -S localhost: 4443 -t. route.php

route.php:

$f=preg_replace('/\/$/','',__DIR__."{$_SERVER['REQUEST_URI']}");
if(is_dir($f)){
$abf=preg_replace('/\/$/','',$_SERVER['REQUEST_URI']);
$fs=glob("$f/*");
foreach($fs as $one){
    $one=str_replace($f.'/','',$one);
    echo "<a href='$abf$one'>$one</a><br/>";
}
    return true;
}else{
    return false;
}
0

, , .

I am to blame for using an integrated web server not only for debugging, but also for exchanging files from random folders on my computer with virtual machines or another computer on the computer.

It is said so, here is my update script (its cam is used with "php -S localhost: 8080 -t. Script.php" from any folder):

<?php
$dir = (isset($_GET['dir']) ? $_GET['dir'] : ".");

if (is_dir($dir)) {
  echo "<h2>Index of $dir:</h2>\n";
  $files = array_diff(scandir($dir), array('..', '.'));
  usort($files, function($a, $b) {
    if(is_dir($a) == is_dir($b))
      return strnatcasecmp($a, $b);
    else
      return is_dir($a) ? -1 : 1;
  });
  echo "<a href='?dir=" . urlencode(dirname($dir)) . "'>..</a><br>\n";
  foreach($files AS $file) {
    echo "<a href='?dir=" . urlencode("{$dir}/{$file}") . "'>{$file}</a><br>\n";                                                                                                                                                                                                                                                                       
  }                                                                                                                                                                                                                                                                                                                                                    
} else if (is_file($dir)) {                                                                                                                                                                                                                                                                                                                            
  header('Content-Description: File Transfer');                                                                                                                                                                                                                                                                                                        
  header('Content-Type: application/octet-stream');                                                                                                                                                                                                                                                                                                    
  header('Content-Disposition: attachment; filename="' . basename($dir) . '"');                                                                                                                                                                                                                                                                        
  header('Expires: 0');                                                                                                                                                                                                                                                                                                                                
  header('Cache-Control: must-revalidate');                                                                                                                                                                                                                                                                                                            
  header('Pragma: public');                                                                                                                                                                                                                                                                                                                            
  header('Content-Length: ' . filesize($dir));                                                                                                                                                                                                                                                                                                         
  readfile($dir);
}
exit;
0
source

All Articles