Web 在线文件管理器学习笔记与总结(3)创建文件

① 创建文件

a. 文件名的合法性:不能包含 \/:*"<>| 等特殊字符

b. 检测当前目录下是否存在同名文件,如果存在提示请重命名后创建,如果不存在则直接创建

index.php:

<?php
require ‘dir.func.php‘;
require ‘file.func.php‘;
require ‘common.func.php‘;
$path = ‘file‘;
$info = readDirectory($path);

$act = @$_REQUEST[‘act‘];
$filename = @$_REQUEST[‘filename‘];
//跳转变量
$redirect = "index.php?path={$path}";
if($act == ‘createFile‘){
    //创建文件
    $mes = createFile($path.‘/‘.$filename);
    alertMes($mes,$redirect);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="cikonss.css" />
<link rel="stylesheet" href="common.css" />
</head>
<body>
<h1>在线文件管理器</h1>
<div id="top">
    <ul id="navi">
        <li><a href="index.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
        <li><a href="#" onclick="show(‘createFile‘)" title="新建文件" ><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-file"></span></span></a></li>
        <li><a href="#" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
        <li><a href="#" title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
        <li><a href="#" title="返回上级目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-arrowLeft"></span></span></a></li>
    </ul>
</div>
<form action="index.php" method="post" enctype="multipart/form-data">
<table width=‘100%‘ border=‘1‘ cellpadding="5" cellspacing="0" bgcolor="#abcdef" align="center">
    <tr id="createFolder"  style="display:none;">
        <td>请输入文件夹名称</td>
        <td >
            <input type="text" name="dirname" />
            <input type="hidden" name="path"  value="<?php echo $path;?>"/>
            <input type="submit"  name="act"  value="创建文件夹"/>
        </td>
    </tr>
    <tr id="createFile"  style="display:none;">
        <td>请输入文件名称</td>
        <td >
            <input type="text"  name="filename" />
            <input type="hidden" name="path" value="<?php echo $path;?>"/>
            <input type="hidden" name=‘act‘ value=‘createFile‘/>
            <input type="submit" value="创建文件" />
        </td>
    </tr>
    <tr id="uploadFile" style="display:none;">
        <td >请选择要上传的文件</td>
        <td ><input type="file" name="myFile" />
            <input type="submit" name="act" value="上传文件" />
        </td>
    </tr>
    <tr align="center">
        <td>编号</td>
        <td>名称</td>
        <td>类型</td>
        <td>大小</td>
        <td>可读</td>
        <td>可写</td>
        <td>可执行</td>
        <td>创建时间</td>
        <td>修改时间</td>
        <td>访问时间</td>
        <td>操作</td>
    </tr>
    <?php
        if($info[‘file‘]){
            $i = 1;
            foreach($info[‘file‘] as $val){
                $p = $path.‘/‘.$val;
    ?>
    <tr align="center">
        <td><?php echo $i;?></td>
        <td><?php echo $val;?></td>
        <td><?php $src = filetype($p) == ‘file‘?‘file_ico.png‘:‘folder_ico.png‘;?><img src="images/<?php echo $src;?>"  title=‘文件‘></td>
        <td><?php echo transByte(filesize($p));?></td>
        <td><?php $src = is_readable($p)?‘correct.png‘:‘error.png‘;?><img src="images/<?php echo $src;?>" width="32"  title=‘可读‘></td>
        <td><?php $src = is_writeable($p)?‘correct.png‘:‘error.png‘;?><img src="images/<?php echo $src;?>" width="32"  title=‘可写‘></td>
        <td><?php $src = is_executable($p)?‘correct.png‘:‘error.png‘;?><img src="images/<?php echo $src;?>" width="32"  title=‘可写‘></td>
        <td><?php echo date(‘Y-m-d H:i:s‘,filectime($p));?></td>
        <td><?php echo date(‘Y-m-d H:i:s‘,filemtime($p));?></td>
        <td><?php echo date(‘Y-m-d H:i:s‘,fileatime($p));?></td>
        <td>
            <a href="" title=‘查看‘><img src="images/show.png" width="32" ></a>
            <a href="" title=‘修改‘><img src="images/edit.png" width="32" ></a>
            <a href="" title=‘重命名‘><img src="images/rename.png" width="32" ></a>
            <a href="" title=‘复制‘><img src="images/copy.png" width="32" ></a>
            <a href="" title=‘剪切‘><img src="images/cut.png" width="32" ></a>
            <a href="" title=‘删除‘><img src="images/delete.png" width="32" ></a>
            <a href="" title=‘下载‘><img src="images/download.png" width="32" ></a>
        </td>
    </tr>
    <?php
                $i++;
            }
        }
    ?>
</table>
</form>
<script src=‘common.js‘></script>
</body>
</html>

common.func.php 存放公共方法:

<?php
//提示操作信息并跳转
function alertMes($mes,$url){
    echo "<script>alert(‘{$mes}‘);location.href=‘{$url}‘;</script>";
}

common.js:

function show(dis){
    document.getElementById(dis).style.display = ‘block‘;
}

file.func.php:

<?php
/*
    转换字节大小
*/
function transByte($size){
    $arr = array(‘B‘,‘KB‘,‘MB‘,‘GB‘,‘TB‘,‘EB‘);
    $i = 0;
    while($size > 1024){
        $size /= 1024;
        $i++;
    }
    return round($size,2).‘ ‘.$arr[$i];
}

/*
    创建文件
*/
/*
注意:
createFile函数前面的正则表达式部分使用了 basename 函数,这个函数会过滤掉所有的 / 斜杠。
这样在文件名中任意输入 / 不会引起报错。
*/
function createFile($filename){
    //验证文件名合法性,是否包含特殊字符 \ / : | * " ? < >
    $pattern = ‘/[\/,\*,<,>,\?\|,\\\\,:,"]/‘; //     * < > | ? : "有效
    if(!preg_match($pattern, basename($filename))){
        //检测当前目录现是否存在同名文件
        if(!file_exists($filename)){
            //通过touch方法创建文件
            if(@touch($filename)){
                return ‘文件创建成功‘;
            }else{
                return ‘文件创建失败‘;
            }
        }else{
            return ‘文件已存在,请重命名后创建‘;
        }
    }else{
        return ‘非法文件名‘;
    }
}    

② 查看文件

③ 修改文件

参考:

正则表达式匹配反斜杠:http://blog.csdn.net/longyulu/article/details/8294114

PHP正则匹配反斜杠‘\‘和美元‘$‘:http://www.oschina.net/code/snippet_583625_20448

时间: 2024-08-05 11:22:01

Web 在线文件管理器学习笔记与总结(3)创建文件的相关文章

Web 在线文件管理器学习笔记与总结(19)上传文件

dir.func.php 中添加方法: /* 上传文件 */ function uploadFile($fileInfo,$path,$allowExt = array('jpg','jpeg','png','gif','txt'),$maxSize = 10487560){ //判断错误号 if($fileInfo['error'] == 0){ //文件是否是http上传上来的 if(is_uploaded_file($fileInfo['tmp_name'])){ $uniqid = md

Web 在线文件管理器学习笔记与总结(5)

① 读出要修改的文件的内容 ② 进行修改 ③ 将修改后的内容写进文件 index.php: <?php require 'dir.func.php'; require 'file.func.php'; require 'common.func.php'; $path = 'file'; $info = readDirectory($path); $act = @$_REQUEST['act']; $filename = @$_REQUEST['filename']; //跳转变量 $redire

Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)

主要函数: filetype() 判断文件类型 filesize() 得到文件大小(字节) is_readable() 判断文件是否可读 is_writeable() 判断文件是否可写 is_executable() 判断文件是否可执行 filectime() 文件创建时间 filemtime() 文件修改时间 fileatime() 文件访问时间 file.func.php 封装文件操作的文件: <?php /* 转换字节大小 */ function transByte($size){ $ar

Web 在线文件管理器学习笔记与总结(13)重命名文件夹(14)复制文件夹

(13)重命名文件夹 ① 重命名文件夹通过 rename($oldname,$newname) 实现 ② 检测文件夹名是否符合规范 ③ 检测当前目录中是否存在同名文件夹名称,如果不存在则重命名成功 index.php: <?php require 'dir.func.php'; require 'file.func.php'; require 'common.func.php'; $path = 'file'; $path = @$_REQUEST['path'][email protected

Web 在线文件管理器学习笔记与总结(11)获取文件夹信息 (12)返回上一级操作

(11)获取文件夹信息 文件夹没有修改操作. index.php: <?php require 'dir.func.php'; require 'file.func.php'; require 'common.func.php'; $path = 'file'; $path = @$_REQUEST['path'][email protected]$_REQUEST['path']:$path; $info = readDirectory($path); if($info == NULL){ e

Web 在线文件管理器学习笔记与总结(4)查看文件内容

② 查看文件内容 a.通过 file_get_contents($filename) 得到文件内容 b.通过 highlight_string($string) 或者 highlight_file($filename) 显示内容.使用 PHP 内置的语法高亮器所定义的颜色,打印输出或者返回输出或者返回语法高亮版本的 PHP 代码 当使用 file_get_contents 读取(php)文件的内容并且显示在页面上,可以使用 <textarea></textarea> 包裹输出的内容

Web 在线文件管理器学习笔记与总结(1)初始文件以及获取首层目录信息

在线文件管理器即使用浏览器管理和操作项目中的目录和文件 文件相关操作包括: 1.创建文件 2.判断文件的权限 3.文件的大小 4.文件的创建时间.修改时间.访问时间 5.查看文件的内容 6.修改文件的内容 7.删除文件 8.重命名文件 9.复制文件 10.剪切文件 11.上传文件 12.下载文件 文件夹相关操作: 1.新建文件夹 2.判断文件夹的权限 3.文件夹的大小 4.文件夹的创建时间.修改时间.访问时间 5.查看文件夹的内容 6.重命名文件夹 7.复制文件夹 8.剪切文件夹 9.文件夹的下

Web 在线文件管理器学习笔记与总结(6)jQuery UI 预览图片

① 查看文件内容,如果文件是图片类型,点击直接查看图片: ② 如果不是图片类型,显示文件中的内容: ③ 使用 jQuery UI 中的 Dialog 显示图片 a.引入: <script src="jquery-1.8.3.min.js"></script> <script src="jquery-ui-1.11.3/jquery-ui.min.js"></script> <link rel="styl

Web 在线文件管理器学习笔记与总结(10)查看文件夹中的内容

① 读取文件夹大小 a. 封装计算文件夹大小的函数 b.  打开文件夹 c. 循环判断文件夹下的内容是文件还是文件夹,如果是文件,则累积相加文件的大小:如果是文件夹,则递归调用该函数 注意两个问题: a. 在计算每个文件夹大小之前,应该清空变量 $size,否则文件夹大小会累加(index.php) <td><?php $size = 0; echo transByte(dirSize($p));?></td> b. 在计算文件夹大小的方法中,$size 应该设置为全局