① 查看文件内容,如果文件是图片类型,点击直接查看图片;

② 如果不是图片类型,显示文件中的内容;

③ 使用 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="stylesheet" href="jquery-ui-1.11.3/jquery-ui.min.css" />

b.通过文件扩展名(或者MIME 类型)来判断是否是图片文件

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);
}else if($act == 'showContent'){
//查看文件内容
$content=file_get_contents($filename);
//echo "<textarea readonly='readonly' cols='100' rows='10'>{$content}</textarea>";
//高亮显示PHP代码
//高亮显示字符串中的PHP代码
if(strlen($content)){
$newContent=highlight_string($content,true);
//高亮显示文件中的PHP代码
//highlight_file($filename);
$str=<<<EOF
<table width='100%' bgcolor='pink' cellpadding='5' cellspacing="0" >
<tr>
<td>$newContent</td>
</tr>
</table>
EOF;
echo $str;
}else{
alertMes("文件没有内容,请编辑再查看!",$redirect);
}
}else if($act == 'editContent'){
$content = file_get_contents($filename);
$str=<<<EOF
<form action='index.php?act=doEdit' method='post'>
<textarea name='content' cols='100' rows='10'>$content</textarea></br>
<input type='hidden' name='filename' value='{$filename}'>
<input type='submit' value='修改文件内容'>
</form>
EOF;
echo $str;
}else if($act == 'doEdit'){
//修改文件内容
$content = $_POST['content'];
if(file_put_contents($filename, $content)){
$mes = '文件修改成功';
}else if(!$content){
$mes = '文件内容被清空';
}else{
$mes = '文件修改失败';
}
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" />
<script src="jquery-1.8.3.min.js"></script>
<script src="jquery-ui-1.11.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="jquery-ui-1.11.3/jquery-ui.min.css" />
</head>
<body>
<div id="showDetail" style="display:none"><img src="" alt="" id="showImg"></div>
<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="data:images/<?php echo $src;?>" alt="" title='文件'></td>
<td><?php echo transByte(filesize($p));?></td>
<td><?php $src = is_readable($p)?'correct.png':'error.png';?><img src="data:images/<?php echo $src;?>" width="32" alt="" title='可读'></td>
<td><?php $src = is_writeable($p)?'correct.png':'error.png';?><img src="data:images/<?php echo $src;?>" width="32" alt="" title='可写'></td>
<td><?php $src = is_executable($p)?'correct.png':'error.png';?><img src="data:images/<?php echo $src;?>" width="32" alt="" 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>
<?php
//得到文件扩展名
$ext = strtolower(end(explode('.',$val)));
$imageExt = array('gif','jpg','png','jpeg');
if(in_array($ext, $imageExt)){ ?>
<a href="javascript:void(0)" onclick='showDetail("<?php echo $val;?>","<?php echo $p;?>")' title='查看'><img src="data:images/show.png" width="32" alt=""></a> <?php }else{ ?>
<a href="index.php?act=showContent&filename=<?php echo $p;?>" title='查看'><img src="data:images/show.png" width="32" alt=""></a>
<?php
}
?> <a href="index.php?act=editContent&filename=<?php echo $p;?>" title='修改'><img src="data:images/edit.png" width="32" alt=""></a>
<a href="" title='重命名'><img src="data:images/rename.png" width="32" alt=""></a>
<a href="" title='复制'><img src="data:images/copy.png" width="32" alt=""></a>
<a href="" title='剪切'><img src="data:images/cut.png" width="32" alt=""></a>
<a href="" title='删除'><img src="data:images/delete.png" width="32" alt=""></a>
<a href="" title='下载'><img src="data:images/download.png" width="32" alt=""></a>
</td>
</tr>
<?php
$i++;
}
}
?>
</table>
</form>
<script src='common.js'></script>
</body>
</html>

c.在 common.js 文件中添加 showDetail() 方法

function show(dis){
document.getElementById(dis).style.display = 'block';
} function showDetail(t,filename){
$("#showImg").attr('src',filename);
$("#showDetail").dialog({
height:"auto",
width:"auto",
position:{my:"center",at:"center",collision:"fit"},
modal:false,//是否模式对话框
draggable:true,//是否允许拖拽
resizable:true,//是否允许缩放
title:t,//对话框标题
show:"slide",
hide:"explode"
});
}

效果图:

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

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

    dir.func.php 中添加方法: /* 上传文件 */ function uploadFile($fileInfo,$path,$allowExt = array('jpg','jpeg','p ...

  2. Web 在线文件管理器学习笔记与总结(17)复制文件 (18)剪切文件

    (17)复制文件 ① 复制文件通过copy($src,$dst) 来实现 ② 检测目标目录是否存在,如果存在则继续检测目标目录中是否存在同名文件,如果不存在则复制成功 file.func.php 中添 ...

  3. Web 在线文件管理器学习笔记与总结(15)剪切文件夹 (16)删除文件夹

    (15)剪切文件夹 ① 通过rename($oldname,$newname) 函数实现剪切文件夹的操作 ② 需要检测目标文件夹是否存在,如果存在还要检测目标目录中是否存在同名文件夹,如果不存在则剪切 ...

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

    (13)重命名文件夹 ① 重命名文件夹通过 rename($oldname,$newname) 实现 ② 检测文件夹名是否符合规范 ③ 检测当前目录中是否存在同名文件夹名称,如果不存在则重命名成功 i ...

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

    (11)获取文件夹信息 文件夹没有修改操作. index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...

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

    ① 读取文件夹大小 a. 封装计算文件夹大小的函数 b.  打开文件夹 c. 循环判断文件夹下的内容是文件还是文件夹,如果是文件,则累积相加文件的大小:如果是文件夹,则递归调用该函数 注意两个问题: ...

  7. Web 在线文件管理器学习笔记与总结(9)下载文件

    ① 普通形式的文件可以使用超链接形式下载 <a href = '下载文件名'>点击下载</a> ② 如果下载图片.html 等类型的文件,使用header() 函数发送网页头信 ...

  8. Web 在线文件管理器学习笔记与总结(8)删除文件

    unlink($filename) 删除文件 index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...

  9. Web 在线文件管理器学习笔记与总结(7)重命名文件

    rename($oldname,$newname) 重命名文件或目录 <<<EOF EOF; 使用heredoc 技术,来部分实现界面与代码的准分离 重命名时,需要验证新文件名的合法 ...

随机推荐

  1. ubuntu用户添加adduser, useradd

    ubuntu和windows一样,可以任意创建或者删除新的用户,windows下比较简单,ubuntu下需要使用命令,不过操作起来不是很繁琐,所以我尽量写的详细一些.  如何创建ubuntu新用户? ...

  2. p235习题3

  3. js之事件冒泡和事件捕获介绍

    链接:http://www.jb51.net/article/42492.htm (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. (2)捕获型事件 ...

  4. AxureRP7.0教学大纲Tutorial directory

    参考: http://www.axurerp.cn/learn/2014/0812/5.html Axure RP7.0标准教程 http://bbs.yuanxingku.com/thread-5- ...

  5. Unrecognized Windows Sockets error: 0: JVM_Bind

    Unrecognized Windows Sockets error: 0: JVM_Bind [转帖]今天很是奇怪,在运行服务器端的时候,经常遇到这个异常: java.net.SocketExcep ...

  6. exec命令

    exec 命令实例 find . -name "*.cc" -exec grep -P -n -H --color=auto "[^\w]main[^\w]" ...

  7. Python实践:开篇

    一.概述 Python实践 是应用Python解决实际问题的案例集合,这些案例中的Python应用通常 功能各异.大小不一. 该系列文章是本人应用Python的实践总结,会不定期更新. 二.目录 Py ...

  8. DOM--1 遵循最佳实践

    为重用命名空间而进行规划 (function() { function $(id) { return document.getElementById(id); } function alertNode ...

  9. 编程第一个Apple Watch程序创建项目

    编程第一个Apple Watch程序创建项目 2.4  编程第一个程序 本节将通过编写第一个程序,为开发者讲解如何添加Watch应用对象.运行程序.界面设计.编写代码等内容本文选自Apple Watc ...

  10. Spring计划

    团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...