Tuesday, December 15, 2009

PHP: list all files of a local directory

... i haven't written an article since march ... here's a short script.

the issue: i wanted to save a list of a music albums i'd converted from CD to mp3 format. to avoid typing it all out i got myself a little script to do the work for me at mouse click:

<pre>
<?php
define('DEFAULT_DIR','D(rive):\\MyFolder\\');
$dir = (isset($_GET['dir'])) ? $_GET['dir'] : DEFAULT_DIR;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo $file . "\n";
}
closedir($dh);
}
}
?>
</pre>


this code simply lists all files and folders of the MyFolder directory.

there was another code snippet i worked on: i wanted a list of the songtitles of a particular album without the .mp3 file extension; the code looks like this:

<?php
define('DEFAULT_DIR','D(rive):/MyFolder');
$dir = (isset($_GET['dir'])) ? $_GET['dir'] : DEFAULT_DIR;
?>
<html>
<head>
<title>List-All-Files : <?=$dir;?></title>
<script language="JavaScript" type="text/javascript">
<!--
function copyit(theField) {
var tempval=eval("document.linktable.list_table");
tempval.focus();
tempval.select();
therange=tempval.createTextRange();
therange.execCommand("Copy");
}
//-->
</script>
<style type="text/css">
<!--
body {font-family:'Trebuchet MS'; color:#AB0000;}
form {font-family:'Courier New', Courier; font-size: 12px;}
.button {font-family: Verdana, Helvetica, sans-serif; font-size: 11px; font-weight: bold; cursor: hand; color: #AB0000; margin-top: 3mm; border: 2px; border-color: #DDDDDD; border-style: outset; background-color: #FFFFFF; width: 150;}
.button1 {font-family: Verdana, Helvetica, sans-serif; font-size: 11px; font-weight: bold; cursor: hand; color: #AB0000; margin-top: 3mm; border: 2px; border-color: #DDDDDD; border-style: outset; background-color: #FFFFFF; width: 150;}
.button2 {font-family: Verdana, Helvetica, sans-serif; font-size: 11px; font-weight: bold; cursor: hand; color: #FFFFFF; margin-top: 3mm; border: 2px; border-color: #DDDDDD; border-style: outset; background-color: #AB0000; width: 150;}
-->
</style>
</head>
<body topmargin="0">
<center><h3><u><i>List-All-Files</i></u> : <?=$dir?></h3></center>
<form name="linktable">
<div><input class="button" type="button" value="COPY & PASTE" onclick="copyit();" onmouseover="this.className='button2'" onmouseout="this.className='button1'"></div><br>
<textarea style="width:875px; height:1000px; color:#FFFFFF; background-color:#AB0000;" name="list_table" readonly="readonly">
<?php
filesInDir($dir);
function filesInDir($tdir)
{
$dirs = scandir($tdir);
foreach($dirs as $file) {
if (($file == '.') || ($file == '..')) { }
elseif (is_dir($tdir.'/'.$file)) { filesInDir($tdir.'/'.$file); }
else { echo substr($file,0,strlen($file)-4)."\n"; } }
}
?>
</textarea>
</form>
</body>
</html>


this will list all files of the directory MyFolder minus the last 4 digits of the complete filename. of course if the file-extension would be .html it would still include the . (dot) in the filename; furthermore, this code displays the list in an easy to use copy and paste form field.

well, at least it's something, isn't it?

tom.paine


No comments: