<?php
/*
*文件配置变量$dirname是目录名称
*/
session_start();
$dirname = 'upload';
$fileClass = new fileClass($dirname);
$fileClass -> fileCMM();/*文件重命名操作*/
$fileClass -> fileDelete();/*文件删除操作*/
?>
<!DOCTYPE html>
<html>
<head>
<mate charset="utf-8"/>
<title>php文件上传操作</title>
<style>
body{font-size:150%;margin:0px;padding:0px;background-color:#FFEBCD;}
.div-top-box{border:solid 1px #008080;width:99.8%;height:100%;background-color:#008080;margin-bottom:10px;}
h1{text-align:center;}
input{font-size:150%;}
.table_top{border-style:solid;border-width:4px 4px 2px 4px;border-color:#708090;padding:60px 10px;width:99%;background-color:#5F9EA0;border-radius:20px;margin:0px auto;}
.table_box{width:98%;margin:0px auto;text-align:center;}
.trColor{background-color:#5F9EA0;padding:10px 5px;border:solid 1px #000;}
.divButton{width:100%;height:200px;border:solid 0px red;background-color:#008080;margin-top:20px;}
.message_box{padding:20px;border-style:solid;border-width:0px 4px 4px 4px;border-color:#708090;background-color:#5F9EA0;margin:0px 4px;border-radius:20px;}
.text-color-red-bold{color:red;font-weight:bold;}
.text-color-red{color:red;}
.text-color-top{color:#00008B;font-weight:bold;}
.text-button{float:right;margin:130px 50px 0px 0px;}
.img{padding:60px 0px 0px 0px;}
.submit{border-radius:20px;width:300px;height:100px;background-color:#FF4500;}
.submit:hover{background-color:#FF8C00;}
.input-box{background-color:#FFD700;}
</style>
</head>
<body style="font-size:150%;">
<div class="div-top-box">
<h1>文件上传操作界面</h1>
</div>
<!--文件上传操作界面HTML界面-->
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="9000000" />
<!-- Name of input element determines name in $_FILES array -->
<table border="0" class="table_top">
<tr>
<td> </td>
<td>
<p class="text-color-red">
*上传图片格式:jpg、gif、png*
</p>
</td>
</tr>
<tr>
<td><p class="text-color-top">请选择文件上传:</p></td>
<td><input name="userfile" type="file" value="文件" class="input-box" />
</td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td></td>
<td><input class="submit" type="submit" value="确定上传" />
</td>
</table>
</form>
<!-----文件操作界面完------->
<!--文件上传操作-->
<div class="message_box">
<pre>
<p class="text-color-red-bold">文件上传提示:</p>
<?php
$fileClass -> fileLoad();/*文件上传操作*/?>
</pre>
</div>
<!--文件上传操作完-->
<?php
/*
*文件遍历操作,并且循环显示在页面上
*/
$num = 0;
$dir_handle = opendir($dirname);
echo '<form action="" method="post">';
echo '<input type="hidden" name="imgsrc" id="imgsrc" value="" />';
echo '<input type="hidden" name="imgname" id="imgname" value="" />';
echo '<table border="0" class="table_box">';
while($file = readdir($dir_handle)){
$dirfile = $dirname.'/'.$file;
if(filetype($dirfile)=='file'){
$num++;
?>
<script>
function delete<?php echo $num ?>(){
if(confirm("你确定要删除该图片吗?")){
var i = document.getElementById("image<?php echo $num ?>").src;
document.getElementById("imgsrc").value = i;
}else{
event.returnValue = false;
}
}
function cmm<?php echo $num ?>(){
var msg = prompt("请输入新文件名(无需输入后缀名):","");
if(msg != '' && msg != null){
document.getElementById("imgname").value = msg;
var i = document.getElementById("image<?php echo $num ?>").src;
document.getElementById("imgsrc").value = i;
}else{
event.returnValue = false;
}
}
</script>
<tr>
<td colspan="2" class="img"><img src="<?php echo $dirfile ?>" width="800px" id="image<?php echo $num ?>"
/>
</td>
</tr>
<tr class="trColor">
<td width="50%" class="trColor">文件名:<?php echo $file ?></td>
<td width="50%" class="trColor">
图像尺寸:
<?php
list($width, $height, $type, $attr) = getimagesize($dirfile);
echo "$width ╳ $height";
?>
</td>
</tr>
<tr>
<td width="50%" class="trColor">
文件大小:<?php echo (round((filesize($dirfile)/1024),2)).'K'
; ?>
</td>
<td width="50%" class="trColor">
上次访问时间:
<?php
date_default_timezone_set('Asia/shanghai');
echo date("Y-n-j H:i:s", fileatime($dirfile))
?>
</td>
</tr>
<tr>
<td width="50%" class="trColor">
<input name="deleteimg" type="submit" value="删除图片" onclick="delete<?php echo $num ?>();" />
</td>
<td width="50%" class="trColor"><input name="cmmimg" type="submit" value="重命名" onclick="cmm<?php echo $num ?>();" /></td>
</tr>
<?php
}
}
echo '</table>';
echo '</form>';
closedir($dir_handle);
?>
<div class="divButton">
<?php
echo '<br/> 在<b>'.$dirname.'</b>目录下的文件共有<b>'.$num.'</b>个。';
?>
<p class="text-button">版权所有:九天一声啸</p>
</div>
</body>
</html>
<?php
/*
*文件操作类
*/
class fileClass{
public $dirName;//目录路径
function __construct($dirName){
$this->dirName = $dirName;
if (!file_exists($this->dirName)) {
if(!mkdir($this->dirName,0777)){
die("<script>alert('创建目录失败!')</script>创建目录失败!");
}
}
}
public function fileCMM(){//文件重命名
function get_extension($file)/*返回文件的后缀名*/
{
return pathinfo($file, PATHINFO_EXTENSION);
}
if(isset($_POST['cmmimg'])){
if($_POST['cmmimg']=='重命名'){
$imgname = $_POST['imgname'];
$imgname_cmm = $this->dirName . '/' .$imgname;
$imgsrc_cmm=basename($_POST['imgsrc']);
$imgsrc_cmm_1 = $this->dirName . '/' .$imgsrc_cmm;
$hzname = get_extension($imgsrc_cmm);
$imgnewname = $imgname_cmm.'.'.$hzname;
if(!file_exists($imgnewname)){
if(!rename($imgsrc_cmm_1,$imgnewname)){
echo "<script>alert('重命名失败!')</script>";
}
}else{
echo "<script>alert('文件名重复,重命名失败!')</script>";
}
}
}
}
public function fileDelete(){//文件删除
if(isset($_POST['deleteimg'])){
if($_POST['deleteimg']=='删除图片'){
$imgsrc=basename($_POST['imgsrc']);
$imgsrc = $this->dirName . '/' .$imgsrc;
if(!unlink($imgsrc)){
echo "<script>alert('删除文件失败!')</script>";
}
}
}
}
public function fileLoad(){//文件上传
if(@$_FILES["userfile"]["name"] <> ''){
switch ($_FILES['userfile']['error']){
case 0: echo '<script>alert("文件上传成功")</script>';
break;
case 1: echo '<script>alert("上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值")</script>';
break;
case 2: echo '<script>alert("上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值")</script>';
break;
case 3: echo '<script>alert("文件只有部分被上传")</script>';
break;
case 4: echo '<script>alert("没有文件被上传")</script>';
break;
case 6: echo '<script>alert("找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进")</script>';
break;
case 7: echo '<script>alert("文件写入失败")</script>';
break;
}
if ((($_FILES["userfile"]["type"] == "image/gif")
|| ($_FILES["userfile"]["type"] == "image/jpeg")
|| ($_FILES["userfile"]["type"] == "image/png")
|| ($_FILES["userfile"]["type"] == "image/pjpeg"))
&& ($_FILES["userfile"]["size"] < 9000000)){
if ($_FILES["userfile"]["error"] > 0){
echo "Return Code: " . $_FILES["userfile"]["error"];
}else{
echo "<br/>上传文件: " . $_FILES["userfile"]["name"];
echo "<br/>文件类型: " . $_FILES["userfile"]["type"];
echo "<br/>文件大小: " . ($_FILES["userfile"]["size"] / 1024) . " Kb";
echo "<br/>临时文件: " . $_FILES["userfile"]["tmp_name"];
echo '<br/>';
if (file_exists("upload/" . $_FILES["userfile"]["name"])){
echo '提示:文件('. $_FILES["userfile"]["name"] . ') 己经存在! ';
$fileN = $_FILES["userfile"]["name"];
echo "<script>var fileN = \"$fileN\";alert('提示:文件“'+fileN+'”己经存在,上传中止!');</script>";
}else{
move_uploaded_file($_FILES["userfile"]["tmp_name"],
$this->dirName .'/' .$_FILES["userfile"]["name"]);
echo "保存位置: " . $this->dirName . '/'. $_FILES["userfile"]["name"];
}
}
}else{
echo '<script>alert("无效的文件")</script>无效的文件!';
}
}else{
echo '提示:没有文件被上传!';
}
}
}
?>