您的位置:首页 > 编程语言 > PHP开发

简易上传文件管理系统

2016-07-12 14:41 477 查看
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息