php实现扫描附件的功能,并判断是否是目录,递归访问目录,返回上一层目录功能的实现
2016-05-25 13:14
645 查看
php实现扫描附件的功能,并判断是否是目录,递归访问目录,返回上一层目录功能的实现
功能描述:
我们在做选择附件上传的时候,可能选择在项目中已经上传过的附件,这样我们就需要扫描文件,选择指定文件并上传。
demo入口代码如下:
~~ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
~~ <html>
~~ <head>
~~ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
~~ <title>文件列表的扫描</title>
~~ <script type="text/javascript" src="./jquery-1.7.1.js"></script>
~~ </head>
~~ <body>
~~
~~ <p>上传文件</p>
~~ <input name='filepath' value='' id='filepath' size='50'/><a href='javascript:;' onclick='selectImage(this)'>浏览</a>
~~ <script type="text/javascript">
~~ function selectImage(e){
~~ var xx = e.pageX || 0;
~~ var yy = e.pageY || 0;
~~ if(xx !=0 && yy !=0 ){
~~ posLeft = xx-100;
~~ posTop = yy;
~~ }else{ var posLeft = 100; var posTop = 100; }
~~ //if(!fname) fname = 'form1.picname';
~~ //if(!stype) stype = '';
~~ window.open("./select_images.php", "popUpImagesWin", "scrollbars=yes,resizable=yes,statebar=no,width=600,height=400,left="+posLeft+", top="+posTop);
~~ }
~~ </script>
~~ </body>
~~ </html>
核心代码的实现代码如下:
~~ <?php
~~ /**
~~ * 文件的扫描
~~ *
~~ */
~~ //文件的存放地址
~~ $uploadPath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
~~ //请求的目录地址
~~ $_GET['activepath'] = isset( $_GET['activepath'] ) ? urldecode($_GET['activepath']) : "";
~~ $activepath = ( isset( $_GET['activepath'] ) && ( trim($_GET['activepath']) ) ) ? trim( $_GET['activepath'] ) : 'upload';
~~
~~ $fileList = scanDirList($uploadPath,$activepath );
~~
~~ //上一级目录
~~ if( $activepath != "" && $activepath != "upload" ){
~~ $preDir = substr( dirname( $uploadPath.$activepath ),strlen( $uploadPath ) );
~~ }else{
~~ $preDir = $activepath;
~~ }
~~ $preDir = urlencode( $preDir );
~~
~~
~~ //文件列表的输出
~~ $fileTrHtml = '';
~~
~~ foreach( $fileList as $k=>$v ){
~~
~~ $fileTrHtml .= "<tr><td>".($v['isdir']==1 ? '【目录】' : '【文件】' );
~~
~~ if( $v['isdir']== 1){
~~ $v['path'] = urlencode( $v['path'] );
~~ $fileTrHtml .= "<a href='./select_images.php?activepath={$v['path']}'>{$v['name']}</a>";
~~
~~ }else{
~~
~~ $fileTrHtml .= "<a href='javascript:;' onclick=\"setImageUrl('{$v['path']}')\">{$v['name']}</a>";
~~
~~ }
~~
~~
~~ $fileTrHtml .= "</td></tr>";
~~ }
~~
~~
~~
~~ $html = <<<EOT
~~ <html>
~~ <head></head>
~~ <body>
~~
~~ <div>
~~ <table>
~~ <tr>
~~ <td><a href="./select_images.php?activepath={$preDir}">返回上一级目录</a> 当前目录{$activepath}</td>
~~ </tr>
~~ {$fileTrHtml}
~~ </table>
~~ </div>
~~ <script type='text/javascript'>
~~ function setImageUrl( imgurl ){
~~ window.opener.document.getElementById('filepath').value = imgurl;
~~ if(document.all) window.opener=true;
~~ window.close();
~~ }
~~ </script>
~~ </body>
~~ </html>
~~ EOT;
~~
~~ echo $html;
~~
~~
~~ /**
~~ *
~~ * @param $uploadPath 文件存储根目录
~~ * @param $activepath 需要扫描的问了名称
~~ */
~~ function scanDirList($uploadPath,$activepath){
~~ $file_array = array();
~~ $path = $uploadPath.$activepath;
~~ if ($handle = @opendir($path)) {
~~ while (false !== ($file = @readdir($handle))) {
~~ if($file=='.' || $file=='..'){
~~ continue;
~~ }
~~ $filePath = $activepath."/".$file;
~~ $temp['name'] = $file;
~~ $temp['path'] = $filePath;
~~ if(is_dir($filePath)){
~~ $temp['isdir'] = 1;
~~ }else{
~~ $temp['isdir'] = 0;
~~ }
~~ $file_array[] = $temp;
~~ }
~~ @closedir($handle);
~~ }
~~ return $file_array;
~~ }
~~
~~
~~ ?>
demo托管地址:[https://github.com/abaiweb/file]
功能描述:
我们在做选择附件上传的时候,可能选择在项目中已经上传过的附件,这样我们就需要扫描文件,选择指定文件并上传。
demo入口代码如下:
~~ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
~~ <html>
~~ <head>
~~ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
~~ <title>文件列表的扫描</title>
~~ <script type="text/javascript" src="./jquery-1.7.1.js"></script>
~~ </head>
~~ <body>
~~
~~ <p>上传文件</p>
~~ <input name='filepath' value='' id='filepath' size='50'/><a href='javascript:;' onclick='selectImage(this)'>浏览</a>
~~ <script type="text/javascript">
~~ function selectImage(e){
~~ var xx = e.pageX || 0;
~~ var yy = e.pageY || 0;
~~ if(xx !=0 && yy !=0 ){
~~ posLeft = xx-100;
~~ posTop = yy;
~~ }else{ var posLeft = 100; var posTop = 100; }
~~ //if(!fname) fname = 'form1.picname';
~~ //if(!stype) stype = '';
~~ window.open("./select_images.php", "popUpImagesWin", "scrollbars=yes,resizable=yes,statebar=no,width=600,height=400,left="+posLeft+", top="+posTop);
~~ }
~~ </script>
~~ </body>
~~ </html>
核心代码的实现代码如下:
~~ <?php
~~ /**
~~ * 文件的扫描
~~ *
~~ */
~~ //文件的存放地址
~~ $uploadPath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
~~ //请求的目录地址
~~ $_GET['activepath'] = isset( $_GET['activepath'] ) ? urldecode($_GET['activepath']) : "";
~~ $activepath = ( isset( $_GET['activepath'] ) && ( trim($_GET['activepath']) ) ) ? trim( $_GET['activepath'] ) : 'upload';
~~
~~ $fileList = scanDirList($uploadPath,$activepath );
~~
~~ //上一级目录
~~ if( $activepath != "" && $activepath != "upload" ){
~~ $preDir = substr( dirname( $uploadPath.$activepath ),strlen( $uploadPath ) );
~~ }else{
~~ $preDir = $activepath;
~~ }
~~ $preDir = urlencode( $preDir );
~~
~~
~~ //文件列表的输出
~~ $fileTrHtml = '';
~~
~~ foreach( $fileList as $k=>$v ){
~~
~~ $fileTrHtml .= "<tr><td>".($v['isdir']==1 ? '【目录】' : '【文件】' );
~~
~~ if( $v['isdir']== 1){
~~ $v['path'] = urlencode( $v['path'] );
~~ $fileTrHtml .= "<a href='./select_images.php?activepath={$v['path']}'>{$v['name']}</a>";
~~
~~ }else{
~~
~~ $fileTrHtml .= "<a href='javascript:;' onclick=\"setImageUrl('{$v['path']}')\">{$v['name']}</a>";
~~
~~ }
~~
~~
~~ $fileTrHtml .= "</td></tr>";
~~ }
~~
~~
~~
~~ $html = <<<EOT
~~ <html>
~~ <head></head>
~~ <body>
~~
~~ <div>
~~ <table>
~~ <tr>
~~ <td><a href="./select_images.php?activepath={$preDir}">返回上一级目录</a> 当前目录{$activepath}</td>
~~ </tr>
~~ {$fileTrHtml}
~~ </table>
~~ </div>
~~ <script type='text/javascript'>
~~ function setImageUrl( imgurl ){
~~ window.opener.document.getElementById('filepath').value = imgurl;
~~ if(document.all) window.opener=true;
~~ window.close();
~~ }
~~ </script>
~~ </body>
~~ </html>
~~ EOT;
~~
~~ echo $html;
~~
~~
~~ /**
~~ *
~~ * @param $uploadPath 文件存储根目录
~~ * @param $activepath 需要扫描的问了名称
~~ */
~~ function scanDirList($uploadPath,$activepath){
~~ $file_array = array();
~~ $path = $uploadPath.$activepath;
~~ if ($handle = @opendir($path)) {
~~ while (false !== ($file = @readdir($handle))) {
~~ if($file=='.' || $file=='..'){
~~ continue;
~~ }
~~ $filePath = $activepath."/".$file;
~~ $temp['name'] = $file;
~~ $temp['path'] = $filePath;
~~ if(is_dir($filePath)){
~~ $temp['isdir'] = 1;
~~ }else{
~~ $temp['isdir'] = 0;
~~ }
~~ $file_array[] = $temp;
~~ }
~~ @closedir($handle);
~~ }
~~ return $file_array;
~~ }
~~
~~
~~ ?>
demo托管地址:[https://github.com/abaiweb/file]
相关文章推荐
- php实时输出内容能够
- 【转】PHP ob_start() 函数介绍
- 在 isilon 中使用 ldap 登录 ftp 服务
- yii2实现根据时间搜索的方法
- Yii2框架使用计划任务的方法
- Windows的程序性能分析和函数呼叫列表工具LTProf (Call Graph at Runtime)
- 安装php-mq扩展
- 提交表单后 PHP获取提交内容的实现方法
- php 文件操作中几种方法整理
- 分享10条PHP性能优化的小技巧,帮助你更好的用PHP开发
- Windows下编译php_scws.php
- phpcms后台批量上传添加图片文章方法详解(一)
- zabbix如何监控php-fpm
- php 解析xml
- NTP时间同步
- PHP 记录日志
- PHP输出json数据时中文不进行unicode编码的几种方法总结
- php 之 文件操作(0524)
- 命令行执行zend framework 2的带参数代码
- win7下配置FTP服务