디렉토리에서 리스트 읽어오기
함수를 이용한 리스트
<html>
<body>
<?php
function get_dirlist($start_dir) {
exec("ls -R $start_dir",$f_list);
$dir_str = $start_dir;
$filelist[0] = $start_dir; $i = 1;
for ($count=0; $count<count($f_list); $count++) {
if ($f_list[$count] == "") { continue; }
if (substr($f_list[$count],strlen($f_list[$count])-1,1) == ":") {
$dir_str = substr($f_list[$count],0,strlen($f_list[$count])-1);
$filelist[$i] = $dir_str;
$i++;
} else {
$file_str = "$dir_str/$f_list[$count]";
if (is_file($file_str)) {
$filelist[$i] = $file_str;
$i++;
}
}
}
return $filelist;
}
?>
<?
$start_dir =".";
$filelist = get_dirlist($start_dir);
for ($c=0; $c<count($filelist); $c++) {
echo iconv("EUC-KR","UTF-8",$filelist[$c]) . "<br>";
}
?>
</body>
</html>
<?
function getFileList($dir)
{
# array to hold return value
$retval = array();
# add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
# open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
# skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry/",
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry")
);
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry",
"type" => mime_content_type("$dir$entry"),
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
$d->close();
return $retval;
}
$dirlist = getFileList("./");
echo "<table>\n";
echo "<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Mod.</th></tr>\n";
foreach($dirlist as $file) {
echo "<tr>\n";
echo "<td>{$file['name']}</td>\n";
//echo "<td>".iconv("EUC-KR","UTF-8",$file['name'])."</td>\n";
//$filename = iconv("EUC-KR","UTF-8",$file['name']);
//echo "<td><a href='$filename'>$filename</a></td>\n";
echo "<td>{$file['type']}</td>\n";
echo "<td>{$file['size']}</td>\n";
echo "<td>" . date("r", $file['lastmod']) . "</td>\n";
echo "</tr>\n";
}
echo "</table>\n\n";
?>
다운로드 받기