目录封装的方法练习

Original 2019-07-28 12:01:06 278
abstract:目录封装的方法练习<?php header('content-type:text/html;charset=utf-8'); date_default_timezone_set('Asia/shanghai'); /**  * 文件创建操作  * @param $fileName  &n

目录封装的方法练习

<?php

header('content-type:text/html;charset=utf-8');
date_default_timezone_set('Asia/shanghai');

/**
 * 文件创建操作
 * @param $fileName   //需要创建的文件名
 * @return string   //提示信息
 */
function create_file($fileName)
{
    if (file_exists($fileName)) {
        return '文件已经存在';
    } elseif (file_exists(dirname($fileName))) {
        touch($fileName);
        return '文件创建成功';
    } elseif (!file_exists(dirname($fileName))) {
        mkdir(dirname($fileName), 0777, true);
        touch($fileName);
        return '文件创建成功';
    } else {
        return '文件创建失败';
    }
}

//echo create_file('text5.txt');
//echo create_file('pp/text5.txt');

/**
 * 文件删除操作
 * @param $fileName   //需要删除的文件名
 * @return string   //提示信息
 */
function del_file($fileName)
{
    if (!file_exists($fileName) && !is_writeable($fileName)) {
        return '文件无法删除';
    } elseif (unlink($fileName)) {
        return '文件删除成功';
    } else {
        return '文件删除失败';
    }
}

//echo del_file('text5.txt');
//echo del_file('pp/text5.txt');

/**
 * 文件复制操作
 * @param $fileName   //需要复制的文件名
 * @param $dest   //复制到的目标目录文件夹
 * @return string   //提示信息
 */
function copy_file($fileName,$dest)
{
    if (!file_exists($fileName) && is_writeable($fileName)) {
        return '文件无法复制';
    } elseif (file_exists($dest)) {
        $destName = $dest .'/' .basename($fileName);
        if (file_exists($destName)) {
            return '文件已经存在';
        } else {
            copy($fileName,$destName);
            return '文件复制成功';
        }
    } elseif (!file_exists($dest)) {
        mkdir($dest,0777,true);
        $destName = $dest .'/' .basename($fileName);
        if (file_exists($destName)) {
            return '文件已经存在';
        } else {
            copy($fileName,$destName);
            return '文件复制成功';
        }
    } else {
        return '文件复制失败';
    }
}

//echo copy_file('text3.txt','pp2');
/**
 * 文件重命名操作
 * @param $oldName   //需要重命名的文件名
 * @param $newName   //新文件名
 * @return string   //提示信息
 */
function rename_file($oldName,$newName)
{
    $path = dirname($oldName);
    $destName = $path .'/' .$newName;
    if (!file_exists($oldName) && !is_writeable($oldName)) {
        return '文件无法重命名';
    } elseif (file_exists($destName)) {
        return '文件名已经存在';
    } elseif (!file_exists($destName)) {
        rename($oldName,$newName);
        return '文件重命名成功';
    } else {
        return '文件重命名失败';
    }
}

//echo rename_file('text1.txt','text5.txt');

/**
 * 文件剪切操作
 * @param $fileName   //要剪切的文件
 * @param $dest   //目标文件目录
 * @return string   //提示信息
 */
function cut_file($fileName,$dest)
{
    $destName = $dest .'/' .basename($fileName);
    if (!is_file($fileName)) {
        return '文件无法剪切';
    } elseif (!is_dir($dest)) {
        mkdir($dest,0777,true);
        if (is_file($destName)) {
            return '文件已经存在';
        } else {
            rename($fileName,$destName);
            return '文件剪切成功';
        }
    } elseif (is_dir($dest)) {
        if (is_file($destName)) {
            return '文件已经存在';
        } else {
            rename($fileName,$destName);
            return '文件剪切成功';
        }
    } else {
        return '文件剪切失败';
    }
}

//echo cut_file('text3.txt','pp3');

/**
 * 文件信息查询操作
 * @param $fileName   //需要查询的文件名
 * @return array|string   //文件信息
 */
function file_get_info($fileName)
{
    if (!is_file($fileName) && is_writeable($fileName)){
        return '文件无法查询信息';
    } else {
        return [
            'type' => filetype($fileName),
            'ctime' => date('Y-m-d H:i:s',filectime($fileName)),
            'mtime' => date('Y-m-d h:i:s',filemtime($fileName)),
            'atime' => date('Y-m-d H:i:s',fileatime($fileName)),
            'size' => trans_byte(filesize($fileName))
        ];
    }
}

//var_dump(file_get_info('text3.txt'));

/**
 * 字节转换操作
 * @param $byte   //字节大小
 * @param int $precision   //保留小数位数
 * @return string
 */
function trans_byte($byte,$precision = 2)
{
    $KB = 1024;
    $MB = 1024 * $KB;
    $GB = 1024 * $MB;
    $TB = 1024 * $GB;
    if ($byte < $KB) {
        return $byte .'B';
    } elseif ($byte < $MB) {
        return round($byte / $KB, $precision) . 'KB';
    } elseif ($byte < $GB) {
        return round($byte / $MB, $precision) . 'MB';
    } elseif ($byte < $TB) {
        return round($byte / $GB, $precision) . 'GB';
    } else {
        return round($byte / $TB, $precision) . 'TB';
    }
}

/**
 * 文件内容读取操作
 * @param $fileName   //需要读取的文件名
 * @return bool|false|string   //文件内容或者提示信息
 */
function read_file($fileName)
{
    if (is_file($fileName) && is_writeable($fileName)) {
        return file_get_contents($fileName);
    } else {
        return '文件无法读取';
    }
}

//echo read_file('text3.txt');

/**
 * 文件内容读取操作(数组返回)
 * @param $fileName   //需要读取的文件名
 * @param bool $skip_empty_lines   //是否有空行
 * @return array|bool|string   //文件内容或者提示信息
 */
function read_file_array($fileName,$skip_empty_lines = false)
{
    if (is_file($fileName) && is_writeable($fileName)) {
        if ($skip_empty_lines = true) {
            return file($fileName,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        } else {
            return file($fileName);
        }
    } else {
        return '文件无法读取';
    }
}

//var_dump(read_file_array('text3.txt'));

/**
 * 文件写入操作
 * @param $fileName   //写入的目标文件
 * @param $data   //写入的数据
 * @param bool $clear   //是否清空目标文件
 * @return string   //提示信息
 */
function write_file($fileName,$data,$clear = false)
{
    $dirname = dirname($fileName);
    if (!is_dir($dirname)) {
        mkdir($dirname,0777,true);
    }
    if (is_array($data) || is_object($data)) {
        $data = serialize($data);
    }
    if ($clear == false) {
        if (is_file($fileName) && is_readable($fileName)) {
            if (filesize($fileName) > 0) {
                $srcData = file_get_contents($fileName);
                echo $srcData;
                $data = $srcData .$data;
            }
        }
    }

    if (file_put_contents($fileName,$data)) {
        return '文件写入成功';
    }
    return '文件写入失败';
}

$data = [
    'name' => '灭绝',
    'age' => 18
];

//var_dump(write_file('text3.txt',$data));

/**
 * 文件下载操作
 * @param $fileName   //需要下载的文件名
 */
function down_file($fileName)
{
    header('Accept-Length:' .filesize($fileName));
    header('Content-Disposition:attachment;filename=' .basename($fileName));
    readfile($fileName);
}

//down_file('text3.txt');

/**
 * 单文件上传操作
 * @param $fileInfo   //上传的文件信息
 * @param string $uploadPath   //上传的指定目录
 * @param array $allowExt   //上传的文件类型
 * @param int $maxSize   //上传文件最大值
 * @return string   //提示信息
 */
function upload_file($fileInfo,$uploadPath = './upload',$allowExt = ['png', 'jpg', 'jpeg', 'gif', 'txt', 'html'],$maxSize = 1000000)
{
    if ($fileInfo['error'] === 0) {
        $ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
        if (!in_array($ext,$allowExt)) {
            return '非法文件类型';
        }
        if ($fileInfo['size'] > $maxSize) {
            return '超出文件上传最大值!';
        }
        if (!is_uploaded_file($fileInfo['tmp_name'])) {
            return '非法上传';
        }
        if (!is_dir($uploadPath)) {
            mkdir($uploadPath,0777,true);
        }
        $fileName = md5(uniqid(microtime(true),true)) .'.' .$ext;
        $dest = $uploadPath .'/' .$fileName ;
        if (!move_uploaded_file($fileInfo['tmp_name'],$dest)) {
            return '文件上传失败';
        }
        return '文件上传成功';
    } else {
        switch ($fileInfo['error']) {
            case 1:
                $res = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!';
                break;
            case 2:
                $res = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!';
                break;
            case 3:
                $res = '文件只有部分被上传!';
                break;
            case 4:
                $res = '没有文件被上传!';
                break;
            case 6:
                $res = '找不到临时文件夹';
                break;
            case 7:
                $res = '文件写入失败';
                break;
        }
        return $res;
    }
}

/*-------------------------------------------目录操作方法-------------------------------------------*/
/**
 * 目录创建操作
 * @param $dirName   //需要创建的目录名称
 * @return string   //提示信息
 */
function create_folder($dirName)
{
    if (file_exists($dirName)) {
        return '存在相同的文件';
    } elseif (mkdir($dirName,0777,true)) {
        return '目录创建成功';
    } else {
        return '目录创建失败';
    }
}

//echo create_folder('pp4/1');

/**
 * 目录删除操作
 * @param $path   //需要要删除的目录
 * @return string   //提示信息
 */
function del_folder($path)
{
    $dir = opendir($path);
    while ($item = readdir($dir)) {
        if ($item != "." && $item != "..") {
            if (is_file($path ."/" .$item)) {
                unlink($path ."/" .$item);
            }
            if (is_dir($path ."/" .$item)) {
                $func = __FUNCTION__;
                $func($path ."/" .$item);
            }
        }
    }
    closedir($dir);
    rmdir($path);
    return '目录删除成功';
}

//echo del_folder('pp4');

/**
 * 目录复制操作
 * @param $src   //源目录
 * @param $dest   //目标目录
 * @return string   //提示信息
 */
function copy_dir($src,$dest)
{
    if (!file_exists($dest)) {
        mkdir($dest,0777,true);
    } else {
        return '该目录下存在相同的文件';
    }
    $dir = opendir($src);
    while ($item = readdir($dir)) {
        if ($item != "." && $item != "..") {
            if (is_file($src ."/" .$item)) {
                copy($src ."/" .$item,$dest ."/" .$item);
            }
            if (is_dir($src ."/" .$item)) {
                $func = __FUNCTION__;
                $func($src ."/" .$item,$dest ."/" .$item);
            }
        }
    }
    closedir($dir);
    return '目录复制成功';
}

//echo copy_dir('pp3','pp/pp3');

/**
 * 目录重命名操作
 * @param $oldName   //源目录名
 * @param $newName   //新目录名
 * @return string    //提示信息
 */
function rename_dir($oldName,$newName)
{
    if (!file_exists($newName)) {
        if (rename($oldName,$newName)) {
            return '重命名成功';
        } else {
            return '重命名失败';
        }
    }
    return '已存在该文件或者目录';
}

//echo rename_dir('pp','pp55');

/**
 * 目录剪切操作
 * @param $src   //源目录
 * @param $dest   //目标目录
 * @return string   //提示信息
 */
function cut_dir($src,$dest)
{
    if (is_dir($dest)) {
        $dest = $dest ."/" .$src;
        if (!file_exists($dest)) {
            if (rename($src,$dest)) {
                return '目录剪切成功';
            } else {
                return '目录剪切失败';
            }
        } else {
            return '该目录下已存在此文件';
        }
    } else {
        return '目标不是目录';
    }
}

//echo cut_dir('text3.txt','pp55');

/**
 * 目录读取操作
 * @param $path   //要读取的目录
 * @return array   //数组|失败
 */
function read_dir($path)
{
    $arr = [];
    $dir = opendir($path);
    while ($item = readdir($dir)) {
        if ($item != "." && $item != "..") {
            if (is_file($path ."/" .$item)) {
                $arr['file'][] = $item;
            }
            if (is_dir($path ."/" .$item)) {
                $arr['dir'][] = $item;
            }
        }
    }
    closedir($dir);
    return $arr;
}

//var_dump(read_dir('pp55'));

/**
 * 目录大小查询操作
 * @param $path   //需要查询大小的目录
 * @return false|int   //目录大小
 */
function dir_size($path)
{
    $sum = 0;
    global $sum;
    $dir = opendir($path);
    while ($item = readdir($dir)) {
        if ($item != "." && $item != "..") {
            if (is_file($path ."/" .$item)) {
                $sum += filesize($path ."/" .$item);
            }
            if (is_dir($path ."/" .$item)) {
                $func = __FUNCTION__;
                $func($path ."/" .$item);
            }
        }
    }
    return $sum;
}

echo trans_byte(dir_size('pp55'));


Correcting teacher:天蓬老师Correction time:2019-07-30 09:27:55
Teacher's summary:从php5.4开始, 提供了SPL函数库, 里里有一些有用的文件类操作的封装, 有空可以了解一下

Release Notes

Popular Entries