您的位置:首页 > 编程语言 > PHP开发

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: