PHP文件上传和文件操作案例
<?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 '提示:没有文件被上传!'; }
} }
?>
PHP文件上传和文件操作案例的更多相关文章
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- webAPI文件上传时文件过大404错误的问题
背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...
- springmvc文件上传下载简单实现案例(ssm框架使用)
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...
- Uploadify多文件上传插件.NET使用案例+PHP使用案例
ploadify是一个非常好用的多文件上传插件 插件下载:http://www.uploadify.com 下载后需要用到的文件: 接下来就是直接添加代码: Default.aspx代码 <%@ ...
- 【C#公共帮助类】FTPClientHelper帮助类,实现文件上传,目录操作,下载等动作
关于本文档的说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行 欢迎传播分享,必须保持原作者的信息,但禁止将该文档直接用于商业盈利. 本人自从几年前走上编程之路,一直致力于收集 ...
- C# FTPClientHelper共公类 实现文件上传,目录操作,下载等动作
文档说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行 1.基本介绍 由于最近的项目是客户端的程序,需要将客户端的图片文件[切图]-[打包]-[ftp上传],现在就差最后一步了 ...
- 【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第四篇博 ...
随机推荐
- ural1855 Trade Guilds of Erathia
Trade Guilds of Erathia Time limit: 2.0 secondMemory limit: 64 MB The continent of Antagarich was co ...
- CharSequence的getText()与String的getString()(转)
CharSequence的getText()与String的getString()『Android系列七』 曾经在学习中碰见两种获取常量的方式: CharSequence chrs = getText ...
- Andorid APK反逆向解决方案---梆梆加固原理探寻
本文章由Jack_Jia编写,转载请注明出处. 文章链接:http://blog.csdn.net/jiazhijun/article/details/8892635 作者:Jack_Jia ...
- 在IE6里面当元素浮动后再设置margin那么就会产生双倍边距
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Android学习笔记之Intent(1)
1.Intent指定启动目标组件 2.Intentfilter描述基本组件所在地址 3.其他包引入资源文件时记得引入R所在的包 package com.jikexueyuan.intent; impo ...
- spring boot + neo4j restful
整整折腾了三天,终于把spring boot + neo4j的路走通了. 这里介绍3个部分,pom,entity,repository 1)pom <?xml version="1.0 ...
- Ubuntu Server PHP常用扩展库的安装
sudo apt-get install php5-gd curl libcurl3 libcurl3-dev php5-curl
- 我收藏的Blog
收集我开发过程中遇见的优秀Blog iOS圈 王巍-强烈推荐 唐巧-强烈推荐 YYKit作者-强烈推荐 Imrazor's Blog Ryan's Zone http://www.cnblogs.co ...
- spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- Quick Cocos2dx 与 EnterFrame事件
利用EnterFrame做出行走的效果,效果图如下: 具体操作: 1 给self多加一个bg1用作与bg无限循环换位 2 在AnotherScene:onEnter方法里面新增onEnterFrame ...