php实现扫描附件的功能,并判断是否是目录,递归访问目录,返回上一层目录功能的实现

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:;' οnclick='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:;' οnclick=\"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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当前目录{$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]
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值