php 关于文件夹的一些封装好的函数

/**
 *检测文件名是否合法
 * @param string $filename
 * @return boolean
 */
function checkFilename($filename){
    $pattern = "/[\/,\*,<>,\?\|]/";
    if (preg_match ( $pattern,  $filename )) {
        return false;
    }else{
        return true;
    }
}
<?php
//打开指定目录
/**
 * 遍历目录函数,只读取目录中的最外层的内容
 * @param string $path
 * @return array
 */
function readDirectory($path) {
    $handle = opendir ( $path );
    while ( ($item = readdir ( $handle )) !== false ) {
        //.和..这2个特殊目录
        if ($item != "." && $item != "..") {
            if (is_file ( $path . "/" . $item )) {
                $arr [‘file‘] [] = $item;
            }
            if (is_dir ( $path . "/" . $item )) {
                $arr [‘dir‘] [] = $item;
            }

        }
    }
    closedir ( $handle );
    return $arr;
}
//$path="file";
//print_r(readDirectory($path));

/**
 * 得到文件夹大小
 * @param string $path
 * @return int
 */
function dirSize($path){
    $sum=0;
    global $sum;
    $handle=opendir($path);
    while(($item=readdir($handle))!==false){
        if($item!="."&&$item!=".."){
            if(is_file($path."/".$item)){
                $sum+=filesize($path."/".$item);
            }
            if(is_dir($path."/".$item)){
                $func=__FUNCTION__;
                $func($path."/".$item);
            }
        }

    }
    closedir($handle);
    return $sum;
}
//$path="file";
//echo dirSize($path);

function createFolder($dirname){
    //检测文件夹名称的合法性
    if(checkFilename(basename($dirname))){
        //当前目录下是否存在同名文件夹名称
        if(!file_exists($dirname)){
            if(mkdir($dirname,0777,true)){
                $mes="文件夹创建成功";
            }else{
                $mes="文件夹创建失败";
            }
        }else{
            $mes="存在相同文件夹名称";
        }
    }else{
        $mes="非法文件夹名称";
    }
    return $mes;
}
/**
 * 重命名文件夹
 * @param string $oldname
 * @param string $newname
 * @return string
 */
function renameFolder($oldname,$newname){
    //检测文件夹名称的合法性
    if(checkFilename(basename($newname))){
        //检测当前目录下是否存在同名文件夹名称
        if(!file_exists($newname)){
            if(rename($oldname,$newname)){
                $mes="重命名成功";
            }else{
                $mes="重命名失败";
            }
        }else{
            $mes="存在同名文件夹";
        }
    }else{
        $mes="非法文件夹名称";
    }
    return $mes;
}

function copyFolder($src,$dst){
    //echo $src,"---",$dst."----";
    if(!file_exists($dst)){
        mkdir($dst,0777,true);
    }
    $handle=opendir($src);
    while(($item=readdir($handle))!==false){
        if($item!="."&&$item!=".."){
            if(is_file($src."/".$item)){
                copy($src."/".$item,$dst."/".$item);
            }
            if(is_dir($src."/".$item)){
                $func=__FUNCTION__;
                $func($src."/".$item,$dst."/".$item);
            }
        }
    }
    closedir($handle);
    return "复制成功";

}

/**
 * 剪切文件夹
 * @param string $src
 * @param string $dst
 * @return string
 */
function cutFolder($src,$dst){
    //echo $src,"--",$dst;
    if(file_exists($dst)){
        if(is_dir($dst)){
            if(!file_exists($dst."/".basename($src))){
                if(rename($src,$dst."/".basename($src))){
                    $mes="剪切成功";
                }else{
                    $mes="剪切失败";
                }
            }else{
                $mes="存在同名文件夹";
            }
        }else{
            $mes="不是一个文件夹";
        }
    }else{
        $mes="目标文件夹不存在";
    }
    return $mes;
}

/**
 * 删除文件夹
 * @param string $path
 * @return string
 */
function delFolder($path){
    $handle=opendir($path);
    while(($item=readdir($handle))!==false){
        if($item!="."&&$item!=".."){
            if(is_file($path."/".$item)){
                unlink($path."/".$item);
            }
            if(is_dir($path."/".$item)){
                $func=__FUNCTION__;
                $func($path."/".$item);
            }
        }
    }
    closedir($handle);
    rmdir($path);
    return "文件夹删除成功";
}
时间: 2024-08-15 11:24:44

php 关于文件夹的一些封装好的函数的相关文章

VC如何得到一个文件夹的路径

VC中没有现成的函数来选择一个文件夹,但这是经常会用到的,怎么办?自动动手,丰衣足食! 使用SHBrowseForFolder,代码如下: #include   int SelFolder(HWND hParent, CString &strFolder){    strFolder.Empty();     LPMALLOC lpMalloc;     if (::SHGetMalloc(&lpMalloc) != NOERROR) return 0;     char szDispla

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

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

android打开文件、保存对话框、创建新文件夹对话框(转载)

转载地址:点击打开 这是一个简单的只有3个按钮的程序,3个按钮分别对应三种工作的模式(保存.打开和文件夹选择).封装的SimpleFileDialog.java的内容如下: package com.example.test; /* * * This file is licensed under The Code Project Open License (CPOL) 1.02 * http://www.codeproject.com/info/cpol10.aspx * http://www.c

php如何判断一个是文件还是文件夹

除了file_exsits() 之外 文件函数我们还有用到很多.不指判断 是否有文件, 还可以判断 是否为文件夹或文件  用于日常 图片显示 很有用, 如果判断 图片是否存在, 如果 不存在就显示 默认图等.. php判断文件还是文件夹主要通过is_file跟is_dir函数判断,下面分别讲解: is_file()函数 is_file()函数 用来判断是否为文件,返回结果为true或者false 举例: $ifile="c:/test"; $result=is_file($ifile)

简单的批量提取文件夹内文件名的经典方法

一个文件夹下面有很多文件,如果要批量提取文件夹内所有文件名,知道如何操作吗? 比如,笔者电脑里面E盘文件夹下面有一个“4.2函数经典再现”的文件夹,想将这个文件夹下面的所有文件名批量提取到一个记事本文件里面. 如果你也想批量提取文件名(批量获取一个文件夹内所有文件的文件名),可以按下面的步骤进行. 第一步,新建一个txt格式的记事本文件. 第二步,在记事本文件中输入:DIR *.*  /B >LIST.TXT 第三步,将此记事本文件后辍名,由txt改为bat.会弹出重命名对话框,单击“是”. 第

C++开发人脸性别识别教程(8)——搭建MFC框架之读取文件夹信息

在上一篇博客中我们已经绘制了MFC界面,在这篇博客中我们将添加响应代码,为MFC框架添加一个最基本的功能:打开一个文件夹. 一.添加相关头文件 这里头文件主要包含三类:opencv头文件.批量读取文件相关的头文件.CvvImage.这里需要强调CvvImage这个头文件,这个是用来关联OpenCv和picture控件,并且这个头文件是隶属于OpenCv1.x的,在2.x版本中已经将这个类移除,因此需要手动下载这两个文件(CvvImage.h和CvvImage.cpp),下载地址:CvvImage

linux文件夹操作及递归遍历文件夹

文件夹相关函数介绍 //mkdir 函数创建文件夹 #include <sys/stat.h> #include <sys/types.h> int mkdir(const char *pathname, mode_t mode); //rmdir 删除文件夹 #include <unistd.h> int rmdir(const char *pathname); //dopendir/fdopendir  //打开文件夹 DIR是一个结构体,是一个内部结构,用来存储读

C#WinForm treeview 简单文件夹管理器 查看文件夹下的文件,子文件下的文件

1 查看的文件夹中的内容 2 UI 3 代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 usi

LabVIEW - 获取当前VI所在文件夹路径、电子表格记录数据

使用函数"当前VI路径",获取当前VI所在的路径,其中包含文件名和扩展名: 使用函数"截取字符串",获取当前VI所在文件夹的绝对路径: 使用函数"字符串至路径转换",将编辑好的文件路径字符串转换为路径: 使用函数"写入电子表格文件",将数组写入电子表格文件,在使用过程中需确定'添加至新文件?'和'转置?'选项是否需要置为真: