簡易上傳文件管理系統

<?php
if (isset($_GET['type'])) {
    $path = "attachment/" . date("Ymd");
    if ($_GET['type'] == "0") {
        if (!file_exists($path)) {
            mkdir($path);
        }
        if (substr($_FILES["file"]["name"], -4, 4) == ".php") {
            echo "<span style=\"color:red\">上傳失敗!</span>  <a href=\"index.php\">返回</a>";
        } else {
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $path . "/" . iconv("UTF-8", "GBK//IGNORE", $_FILES["file"]["name"]))) {
                echo "<span style=\"color:red\">上傳成功!</span>  <a href=\"index.php\">返回</a>";
            } else {
                echo "<span style=\"color:red\">上傳失敗!</span>  <a href=\"index.php\">返回</a>";
            }
        }
    } else {
        if (unlink($path . "/" . iconv("UTF-8", "GBK//IGNORE", $_GET["filename"]))) {
            echo "<span style=\"color:red\">刪除成功!</span>  <a href=\"index.php\">返回</a>";
        } else {
            echo "<span style=\"color:red\">刪除失敗!</span>  <a href=\"index.php\">返回</a>";
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>上傳文件管理</title>
<style type="text/css">
a{
	color:#000;
	text-decoration:none;
}
</style>
<script type="text/javascript">
function check(form) {
    if (form.file.value == "") {
        alert("請選擇文件!");
        return false;
    }
    return true;
}
function del(filename) {
    if (confirm("確認要刪除 " + filename + " 嗎?")) {
        window.location.href = "?type=1&filename=" + filename;
    }
}
</script>
</head>

<body>
<form action="?type=0" method="post" enctype="multipart/form-data" name="form1" onsubmit="return check(this);">
  <label for="file"></label>
  <input type="file" name="file" id="file" style="width:348px;height:24px">
  <input type="submit" name="button" id="button" value="上傳">
</form>
<br />
<table width="400" border="1" cellpadding="0" cellspacing="0" style="text-align:center">
  <tr>
    <td width="70%">文件名</td>
    <td colspan="2">操作</td>
  </tr>
<?php
$path = "attachment/" . date("Ymd");
if (file_exists($path)) {
    $filenames = scandir($path);
    foreach ($filenames as $filename) {
        if ($filename != "." && $filename != "..") {
            $name = iconv("GBK", "UTF-8", $filename);
            echo "<tr>";
            echo "<td style=\"text-align:left\">" . $name . "</td>";
            echo "<td><a href=\"javascript:;\" onclick=\"window.open('" . $path . "/" . $name . "');\">下載</a></td>";
            echo "<td><a href=\"javascript:;\" onclick=\"del('" . $name . "');\">刪除</a></td>";
            echo "</tr>";
        }
    }
}
?>
</table>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章