:::

8-2 index.php 中文目錄修正

<?php
$base_dir="D:/httpd/09/uploads";
$base_url="http://localhost/09/uploads";

$_REQUEST['to']=mb_convert_encoding($_REQUEST['to'], "Big5", "Big5,UTF-8");

$dir=(empty($_REQUEST['to']))?$base_dir:str_replace("\\","/",realpath($_REQUEST['to']));
$dir=(ereg("^".$base_dir,$dir))?$dir:$base_dir;
$url=str_replace($base_dir,$base_url,$dir);

define("_UPLOAD_DIR",$dir);
define("_UPLOAD_URL",$url);

if($_GET['op']=="del"){
    $file=mb_convert_encoding($_GET['file'], "Big5", "Big5,UTF-8");
    @unlink(_UPLOAD_DIR."/".$file);
    header("location:"._UPLOAD_DIR);
}elseif($_POST['op']=="del"){
        if(is_array($_POST['files'])){
        foreach($_POST['files'] as $file){
            $file=mb_convert_encoding($file, "Big5", "Big5,UTF-8");
            @unlink(_UPLOAD_DIR."/".$file);
        }
        header("location:"._UPLOAD_DIR);
    }
}elseif($_POST['op']=="move"){
        if(is_array($_POST['files'])){
        foreach($_POST['files'] as $file){
            $file=mb_convert_encoding($file, "Big5", "Big5,UTF-8");
            @rename(_UPLOAD_DIR."/".$file,$_POST['new_folder']."/".$file);
        }
        header("location:"._UPLOAD_DIR);
    }
}elseif(!empty($_POST['mk_folder'])){
        $mk_folder=mb_convert_encoding($_POST['mk_folder'], "Big5", "Big5,UTF-8");
      mk_dir(_UPLOAD_DIR."/{$mk_folder}");
    header("location:"._UPLOAD_DIR);
}elseif(!empty($_FILES['pic']['tmp_name'])){
  foreach($_FILES['pic']['tmp_name'] as $i => $tmpname){
        $up_pic=mb_convert_encoding($_FILES['pic']['name'][$i], "Big5", "Big5,UTF-8");
    move_uploaded_file($tmpname , _UPLOAD_DIR."/{$up_pic}");
  }
  header("location:"._UPLOAD_DIR);
}

$dh=opendir(_UPLOAD_DIR);


$show_dir=mb_convert_encoding($dir, "UTF-8", "Big5,UTF-8");


$main="
<script src='js/jquery-1.3.2.min.js' type='text/javascript'></script>
<script src='js/jquery.MultiFile.js' type='text/javascript'></script>
<form action='{$_SERVER['PHP_SELF']}' method='post'  enctype='multipart/form-data'>
<table class='tinytable'>
<tr><td colspan=4>$show_dir</td></tr>
<tr><th>檔名</th><th>大小</th><th>類型</th><th>功能</th></tr>";

while($file=readdir($dh)){
    if($file==".")continue;
    $type=filetype(_UPLOAD_DIR."/".$file);

    if($type=="dir"){
        $dir_arr[]=$file;
    }else{
        $file_arr[]=$file;
    }
}

//秀出目錄
if(is_array($dir_arr)){
    foreach($dir_arr as $sub_dir){
        $size=($sub_dir=="..")?"":dirSize(_UPLOAD_DIR."/".$sub_dir);
        $size=($sub_dir=="..")?"":formatBytes($size,1);
        $sub_dir=mb_convert_encoding($sub_dir, "UTF-8", "Big5,UTF-8");
        $to_url=urlencode(_UPLOAD_DIR."/".$sub_dir);
        $main.= "<tr><td><a href='index.php?to=$to_url'>$sub_dir</a></td><td>$size</td><td>目錄</td><td></td></tr>";
    }
}

//秀出檔案
if(is_array($file_arr)){
    foreach($file_arr as $file){
        $size=filesize(_UPLOAD_DIR."/".$file);
        $size=formatBytes($size,1);
        $file=mb_convert_encoding($file, "UTF-8", "Big5,UTF-8");
        $main.= "<tr><td><a href='"._UPLOAD_URL."/{$file}'>$file</a></td><td>$size</td><td>檔案</td><td>
            <input type='checkbox' name='files[]' value='$file'>
            <a href='{$_SERVER['PHP_SELF']}?file=$file&op=del& to="._UPLOAD_DIR."'>刪除</a></td></tr>";
    }
}



$to_dir=mb_convert_encoding(_UPLOAD_DIR, "UTF-8", "Big5,UTF-8");

$main.="
<tr><th colspan=4>
<input type='hidden' name='to' value='$to_dir'>
將勾選的檔案 <input type='radio' name='op' value='del'>刪除
<input type='radio' name='op' value='move'>搬到:".folder_select($base_dir,"new_folder")."<br>
建立新目錄:<input type='text' name='mk_folder'><br>
上傳檔案:<input type='file' name='pic[]' class='multi' />
<input type='submit' value='送出'>
</th></tr>
</table>
</form>";
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>
  <link rel="stylesheet" href="tinytable.css" />
  <link rel="stylesheet" href="iconize_l.css" />
  </head>
  <body>
    <?php echo $main;?>
  </body>
</html>




<?php
//計算目錄大小
function dirSize($directory) {
    $size = 0;
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
        $size+=$file->getSize();
    }
    return $size;
}

//把 bytes 轉換成其他單位
function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
}

//目錄選單
function folder_select($base_dir="",$name="",$i=0){
  $prefix=str_repeat("--",$i);
  $i++;
  $option="";
  $dh=opendir($base_dir);
  while($file=readdir($dh)){
    if($file=="." or $file=="..")continue;
    $type=filetype($base_dir."/".$file);
    if($type=="dir"){
        $option.="<option value='{$base_dir}/{$file}'>{$prefix}{$file}</option>";
        $option.=folder_select("{$base_dir}/{$file}",$name,$i);
    }
    }
    if($i==1){
        $main="<select name='$name'>
        $option
        </select>";
    }else{
    $main=$option;
    }
    return $main;
}


//建立目錄
function mk_dir($dir=""){
    //若無目錄名稱秀出警告訊息
    if(empty($dir))die("沒有目錄名稱");
    //若目錄不存在的話建立目錄
    if (!is_dir($dir)) {
        umask(000);
        //若建立失敗秀出警告訊息
        if(!mkdir($dir, 0777)){
            die("$dir 建立失敗!");
        }
    }
}
?>


:::

搜尋


書籍目錄

展開 | 闔起

線上使用者

16人線上 (4人在瀏覽線上書籍)

會員: 0

訪客: 16

更多…