Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP built in server, any way to configure it to show files of directory?

Tags:

php

As yous may be aware, as of PHP 5.4 there is built in server available. However, if you browse to directory with no "index" file, instead if listing all available files/directories (like apache for example), it will give you and error.

Now as far as I understand this is by design and not some sort of bug. But maybe someone knows if there is a way to configure it, to list the contents of directory?

like image 393
galdikas Avatar asked Feb 18 '13 23:02

galdikas


People also ask

How do I get a list of files in a directory in PHP?

The scandir() function returns an array of files and directories of the specified directory.

Where do you put PHP files on a server?

If your server supports PHP, then you do not need to do anything. Just create your . php files, put them in your web directory and the server will automatically parse them for you. There is no need to compile anything nor do you need to install any extra tools.

How directories are used in PHP?

A new directory can be created in PHP using the mkdir() function. This function takes a path to the directory to be created. To create a directory in the same directory as your PHP script simply provide the directory name. To create a new directory in a different directory specify the full path when calling mkdir().

What are listing directories in PHP?

Listing Directory Content in PHP For listing the contents of a directory, we require two of the above-listed PHP directory functions, these are, opendir() and readdir(). There are two steps in directory listing using the PHP program.


2 Answers

For show advanced list of files and directories with sort, date, size, icon type,..., Save bellow content code as route.php file, and then run this command:

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

And then open http://localhost:8080/ in the browser.

Code:

<?php
// This block MUST be at the very top of the page!
@ob_start('ob_gzhandler');
if(isset($_GET['icon']))
{
    $e=$_GET['icon'];
    $I['file']='R0lGODlhEAAPAOYAAIyMlu7u9PHx9vDw9fT0+PPz97u7vvf3+vb2+d/f4vn5+/39/vv7/Pr6+/b29+3t7pCRnI6PmZOVn5ibpZWYopqeqJ2hq6KnsaClr9fZ3ff4+t/g4qSqtKmwuqeuuM3P0vHz9tze4be6vuzv8+vu8urt8eXo7Kuzva61vquyu9/k6uXp7uTo7cvU3dHZ4dDY4Nfe5d3j6dzi6Nvh5+Po7eLn7OHm69HV2ejs8Ofr7+7x9OHk597h5PT2+PP19/Hz9evt7+Lk5t3f4fr7/Pn6+/b3+PX299Tc49rh5+ru8fDz9ff5+vb4+fP19vz9/f////7+/vv7+/Pz8+/v7+z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62as7XK7/26x7ni+/3W38Ha38HKx5nu88XWx5IjD84HA8oTD84vK+ZvP9YnJ963b+bzl+8fs/fT///79mf//r///uf//xPr2k//9pP/4hv/6kPr1kPr1kf/7mvfvgvPkbdm/Ktm/LPn25dm8L/vwvfvzzt61AfbNLNm3KfzUMdm3Mdu5M9q6Nd6+O/3ZR/vdZPzkhfvnl/roodq0Kv7TOP3VOf3aUfzZWPzebfvedPziffrjj/rprfrrs/juytKgB9SjCNWlC8+hDtWmEM+hENKmFNWmFtSnG9WqH9SqH9WqIdWrItiuKOjRh7J9DrR8D7mFKaptDbJ7I6ttDax0HfEZAf///wAAAAAAACH5BAEAAH0ALAAAAAAPABAAAAe8gH2CgzIxLispJyaCdGmOdCY5ODc2NDAsC31pfJxpBJ+gEyMeAWtaT0dISlFSU1R2fSEvAXI6PTw7P0RBQEZ1fRYyAW8+SWVjYmBeW02/GDMBcENMZFhhVlVQzX3P0dPV19nbF9BxQktZV19dXE7bGTUBbEVzbm1qaGdm2xEtAXd59ODZAwrUgw8dAgwStKgECRQqVHRQsHCQiQQIDhwwoLCiRxOLPA7i4KABgwoUJAgQuQFAAQggNIgYEAgAOw==';
    header('Cache-control: max-age=2592000');
    header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T',time()+2592000));
    header('Content-type: image/gif');
    print base64_decode(isset($I[$e])?$I[$e]:$I['file']);
    exit;
}
// End block

// Start configs
$sitename='My files';
$date='M-d-y'; // date format
$ignore=array('.','..','.htaccess','index.php','icon.php','Thumbs.db','web.config'); // ignore these files
// End configs
$root=dirname(__FILE__);
$dir=isset($_GET['dir'])?$_GET['dir']:'';if(strstr($dir,'..'))$dir='';
$path="$root/$dir/";
$dirs=$files=array();
if(!is_dir($path)||false==($h=opendir($path)))exit('Directory does not exist.');
while(false!==($f=readdir($h)))
{
    if(in_array($f,$ignore))continue;
    if(is_dir($path.$f))$dirs[]=array('name'=>$f,'date'=>filemtime($path.$f),'url'=>'index.php?dir='.rawurlencode(trim("$dir/$f",'/')));
    else$files[]=array('name'=>$f,'size'=>filesize($path.$f),'date'=>filemtime($path.$f),'url'=>trim("$dir/".rawurlencode($f),'/'));
}
closedir($h);
$current_dir_name = basename($dir);
$up_dir=dirname($dir);
$up_url=($up_dir!=''&&$up_dir!='.')?'index.php?dir='.rawurlencode($up_dir):'index.php';
// END PHP ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?=$current_dir_name==''?'Directory list':$current_dir_name?></title>
<style type="text/css">
body { font-family: tahoma, verdana, arial; font-size: 0.7em; color: black; padding-top: 8px; cursor: default; background-color: #fff; }
#idx { border: 3px solid #fff; width: 500px; }
#idx td.center { text-align: center; }
#idx td { border-bottom: 1px solid #f0f0f0; }
#idx img { margin-bottom: -2px; }
#idx table { color: #606060; width: 100%; margin-top:3px; }
#idx span.link { color: #0066DF; cursor: pointer; }
#idx .rounded { padding: 10px 7px 10px 10px; -moz-border-radius:6px; }
#idx .gray { background-color:#fafafa;border-bottom: 1px solid #e5e5e5; }
#idx p { padding: 0px; margin: 0px;line-height:1.4em;}
#idx p.left { float:left;width:60%;padding:3px;color:#606060;}
#idx p.right {float:right;width:35%;text-align:right;color:#707070;padding:3px;}
#idx strong { font-family: "Trebuchet MS", tahoma, arial; font-size: 1.2em; font-weight: bold; color: #202020; padding-bottom: 3px; margin: 0px; }
#idx a:link    { color: #0066CC; }
#idx a:visited { color: #003366; }
#idx a:hover   { text-decoration: none; }
#idx a:active  { color: #9DCC00; }
</style>

<script type="text/javascript">
<!--
var _c1='#fefefe'; var _c2='#fafafa'; var _ppg=100; var _cpg=1; var _files=[]; var _dirs=[]; var _tpg=null; var _tsize=0; var _sort='date'; var _sdir={'type':0,'name':0,'size':0,'date':1}; var idx=null; var tbl=null;
function _obj(s){return document.getElementById(s);}
function _ge(n){n=n.substr(n.lastIndexOf('.')+1);return n.toLowerCase();}
function _nf(n,p){if(p>=0){var t=Math.pow(10,p);return Math.round(n*t)/t;}}
function _s(v,u){if(!u)u='B';if(v>1024&&u=='B')return _s(v/1024,'KB');if(v>1024&&u=='KB')return _s(v/1024,'MB');if(v>1024&&u=='MB')return _s(v/1024,'GB');return _nf(v,1)+'&nbsp;'+u;}
function _f(name,size,date,url,rdate){_files[_files.length]={'dir':0,'name':name,'size':size,'date':date,'type':_ge(name),'url':url,'rdate':rdate,'icon':'index.php?icon='+_ge(name)};_tsize+=size;}
function _d(name,date,url){_dirs[_dirs.length]={'dir':1,'name':name,'date':date,'url':url,'icon':'index.php?icon=dir'};}
function _np(){_cpg++;_tbl();}
function _pp(){_cpg--;_tbl();}
function _sa(l,r){return(l['size']==r['size'])?0:(l['size']>r['size']?1:-1);}
function _sb(l,r){return(l['type']==r['type'])?0:(l['type']>r['type']?1:-1);}
function _sc(l,r){return(l['rdate']==r['rdate'])?0:(l['rdate']>r['rdate']?1:-1);}
function _sd(l,r){var a=l['name'].toLowerCase();var b=r['name'].toLowerCase();return(a==b)?0:(a>b?1:-1);}
function _srt(c){switch(c){case'type':_sort='type';_files.sort(_sb);if(_sdir['type'])_files.reverse();break;case'name':_sort='name';_files.sort(_sd);if(_sdir['name'])_files.reverse();break;case'size':_sort='size';_files.sort(_sa);if(_sdir['size'])_files.reverse();break;case'date':_sort='date';_files.sort(_sc);if(_sdir['date'])_files.reverse();break;}_sdir[c]=!_sdir[c];_obj('sort_type').style.fontStyle=(c=='type'?'italic':'normal');_obj('sort_name').style.fontStyle=(c=='name'?'italic':'normal');_obj('sort_size').style.fontStyle=(c=='size'?'italic':'normal');_obj('sort_date').style.fontStyle=(c=='date'?'italic':'normal');_tbl();return false;}

function _head()
{
    if(!idx)return;
    _tpg=Math.ceil((_files.length+_dirs.length)/_ppg);
    idx.innerHTML='<div class="rounded gray" style="padding:5px 10px 5px 7px;color:#202020">' +
        '<p class="left">' +
            '<strong><?=$current_dir_name==''?$sitename:$current_dir_name?></strong><?=$dir!=''?'&nbsp; (<a href="'.$up_url.'">Back</a>)':''?><br />' + (_files.length+_dirs.length) + ' objects in this folder, ' + _s(_tsize) + ' total.' +
        '</p>' +
        '<p class="right">' +
            'Sort: <span class="link" onmousedown="return _srt(\'name\');" id="sort_name">Name</span>, <span class="link" onmousedown="return _srt(\'type\');" id="sort_type">Type</span>, <span class="link" onmousedown="return _srt(\'size\');" id="sort_size">Size</span>, <span class="link" onmousedown="return _srt(\'date\');" id="sort_date">Date</span>' +
        '</p>' +
        '<div style="clear:both;"></div>' +
    '</div><div id="idx_tbl"></div>';
    tbl=_obj('idx_tbl');
}

function _tbl()
{
    var _cnt=_dirs.concat(_files);if(!tbl)return;if(_cpg>_tpg){_cpg=_tpg;return;}else if(_cpg<1){_cpg=1;return;}var a=(_cpg-1)*_ppg;var b=_cpg*_ppg;var j=0;var html='';
    if(_tpg>1)html+='<p style="padding:5px 5px 0px 7px;color:#202020;text-align:right;"><span class="link" onmousedown="_pp();return false;">Previous</span> ('+_cpg+'/'+_tpg+') <span class="link" onmousedown="_np();return false;">Next</span></p>';
    html+='<table cellspacing="0" cellpadding="5" border="0">';
    for(var i=a;i<b&&i<(_files.length+_dirs.length);++i)
    {
        var f=_cnt[i];var rc=j++&1?_c1:_c2;
        html+='<tr style="background-color:'+rc+'"><td><img src="'+f['icon']+'" alt="" /> &nbsp;<a href="'+f['url']+'">'+f['name']+'</a></td><td class="center" style="width:50px;">'+(f['dir']?'':_s(f['size']))+'</td><td class="center" style="width:70px;">'+f['date']+'</td></tr>';
    }
    tbl.innerHTML=html+'</table>';
}
<?php while(list(,$d)=each($dirs))print sprintf("_d('%s','%s','%s');\n",addslashes($d['name']),date($date,$d['date']),addslashes($d['url'])); ?>
<?php while(list(,$f)=each($files))print sprintf("_f('%s',%d,'%s','%s',%d);\n",addslashes($f['name']),$f['size'],date($date,$f['date']),addslashes($f['url']),$f['date']);?>

window.onload=function()
{
    idx=_obj('idx'); _head(); _srt('name');
};
-->
</script>
</head>
<body>
    <div id="idx"><!-- do not remove --></div>
</body>
</html>

Preview:

screenshot file manager

gist

like image 138
Nabi K.A.Z. Avatar answered Oct 06 '22 00:10

Nabi K.A.Z.


Old question, but I feel this could still be useful.

I'm guilty of using the integrate web server not just for debug, but also to share files from random folders on my pc with virtualmachines or other pc in lan.

Said so, here's my updates script (it cam be used with "php -S localhost:8080 -t . script.php" from any folder):

<style>
  a {
    color: black;
    width: 100%;
    display: flex;
    flex-direction: row;
  }

  a:hover {
    background-color: #77AAFF;
    color: black;
  }

  .linkLeft {
    flex-grow: 0;
  }

  .linkCenter {
    flex-grow: 1;
    margin: -3px 10px 3px;
    border-bottom: 1px dotted black;
  }

  .linkRight {
    flex-grow: 0;
  }
</style>
<?php
$dir = realpath(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) use ($dir) {
    if (is_dir("{$dir}/{$a}") == is_dir("{$dir}/{$b}")) {
      return strnatcasecmp($a, $b);
    } else {
      return is_dir("{$dir}/{$a}") ? -1 : 1;
    }
  });
  echo "<a href='?dir=" . urlencode(realpath("{$dir}/..")) . "'>⏎</a>\n";
  foreach ($files as $file) {
    if (is_dir("{$dir}/{$file}")) {
      $typef = "[directory]";
    } else {
      $typef = "[" . pathinfo($file, PATHINFO_EXTENSION) . " file]";
    }
    ?>
    <a href='?dir=<?= urlencode("{$dir}/{$file}"); ?>'>
      <div class='linkLeft'><?= $file; ?></div>
      <div class='linkCenter'></div>
      <div class='linkRight'><?= $typef; ?></div>
    </a>
    <?php
  }
} 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);
}
like image 39
Syco Avatar answered Oct 05 '22 23:10

Syco