[991]PHP網站開發 進階應用技巧2
define("_UPLOAD_DIR","目錄的絕對路徑"); define("_UPLOAD_URL","目錄的網址"); if($dh = opendir(_UPLOAD_DIR)){ while(($file = readdir($dh)) !== false){ if($file==".")continue; $type=filetype(_UPLOAD_DIR."/".$file); if($type=="dir"){ $dir_arr[]=$file; }else{ $file_arr[]=$file; } } closedir($dh); //秀出內容 } |
|
1.把TodunVote1.0.zip解壓縮,並加入css檔。
$main=" <link rel='stylesheet' type='text/css' href='iconize_l.css' /> <link rel='stylesheet' type='text/css' href='tinytable.css' /> <table> <tr><th>名稱</th><th>大小</th><th>類型< /th></tr>"; foreach($dir_arr as $sub_dir){ $dirSize=formatBytes(dirSize(_UPLOAD_DIR."/".$sub_dir)); $main.="<tr> <td><a href='index.php?to="._UPLOAD_DIR."/".$sub_dir."'>$sub_dir</a></td> <td align='right'>$dirSize</td> <td>目錄</td></tr>"; } foreach($file_arr as $file){ $filesize=formatBytes(filesize(_UPLOAD_DIR."/".$file)); $main.="<tr> <td><a href='"._UPLOAD_URL."/".$file."'>$file</a></td> <td align='right'>$filesize</td> <td>檔案</td></tr>"; } $main.="</table>"; |
//主內容變數 |
$base_dir="D:/httpd/07/uploads"; $base_url="http://localhost/07/uploads"; if(!empty($_GET['to'])){ $dir=$_GET['to']; }else{ $dir=$base_dir; } $dir=realpath($dir); $dir=str_replace("\\","/",$dir); define("_UPLOAD_DIR",$dir); define("_UPLOAD_URL",str_replace($base_dir,$base_url,$dir)); |
//重新定義目錄的絕對路徑 |
$len=strlen($base_dir); if(substr($dir,0,$len)!=$base_dir) die("「{$dir}」為不合法路徑"); |
//計算原有目錄絕對路徑長度 |