php文件类
发表于:2024-04-19 18:01:05浏览:177次
前言
本文将详细介绍PHP文件以及文件夹增删改查操作的各个方面,并提供整理的源码和通过示例代码进行说明。
示例
$basicdir = RUNTIME_PATH.'test';
$file = RUNTIME_PATH.'test/商品/说明文档.txt';
$file2 = RUNTIME_PATH.'test/商品copy/说明文档-copy.txt';
$dir = RUNTIME_PATH.'test/商品';
$dir2 = RUNTIME_PATH.'test/商品copy';
// 写入文件
//$res = \app\common\utils\File::write($file,'你好,朋友!');
//dump($res); // 输出=>int(18)
// 读取文件
//$res = \app\common\utils\File::read($file);
//dump($res); // 输出=>string(18) "你好,朋友!"
// 逐行读取到数组中
//$res = \app\common\utils\File::readLine($file);
//dump($res); // 输出=>
//array(1) {
// [0] => string(18) "你好,朋友!"
//}
// 复制文件夹
//$res = \app\common\utils\File::copyDir($dir,$dir2);
//dump($res); // 输出=> bool(true)
// 复制文件
//$res = \app\common\utils\File::copy($file,$file2);
//dump($res); // 输出=> bool(true)
// 判断文件或文件夹是否可写
//$res = \app\common\utils\File::isCanWrite($file);
//dump($res); // 输出=>bool(true)
// 获取文件信息
//$res = \app\common\utils\File::fileinfo($file);
//dump($res); // 输出=>
//array(9) {
// ["basename"] => string(16) "说明文档.txt"
// ["filename"] => string(12) "说明文档"
// ["extension"] => string(3) "txt"
// ["dirname"] => string(52) "/www/wwwroot/xxx.xxx.xxx/runtime/test/商品"
// ["updatetime"] => int(1713544912)
// ["size"] => int(18)
// ["width"] => int(0)
// ["height"] => int(0)
// ["mime"] => string(0) ""
//}
// 树形结构列出文件夹底下所有文件和文件夹
//$res = \app\common\utils\File::tree($basicdir);
//dump($res); // 输出=>
//array(2) {
// [0] => array(11) {
// ["id"] => string(3) "0_3"
// ["pid"] => string(1) "0"
// ["type"] => string(3) "dir"
// ["name"] => string(6) "商品"
// ["suffix"] => string(0) ""
// ["size"] => int(18)
// ["updatetime"] => int(1713544912)
// ["mime"] => string(0) ""
// ["path"] => string(52) "/www/wwwroot/xxx.xxx.xxx/runtime/test/商品"
// ["nums"] => int(1)
// ["child"] => array(1) {
// [0] => array(11) {
// ["id"] => string(5) "0_3_3"
// ["pid"] => string(3) "0_3"
// ["type"] => string(4) "file"
// ["name"] => string(16) "说明文档.txt"
// ["suffix"] => string(3) "txt"
// ["size"] => int(18)
// ["updatetime"] => int(1713544912)
// ["mime"] => string(0) ""
// ["path"] => string(69) "/www/wwwroot/xxx.xxx.xxx/runtime/test/商品/说明文档.txt"
// ["nums"] => int(0)
// ["child"] => array(0) {
// }
// }
// }
// }
// [1] => array(11) {
// ["id"] => string(3) "0_4"
// ["pid"] => string(1) "0"
// ["type"] => string(3) "dir"
// ["name"] => string(10) "商品copy"
// ["suffix"] => string(0) ""
// ["size"] => int(36)
// ["updatetime"] => int(1713545384)
// ["mime"] => string(0) ""
// ["path"] => string(56) "/www/wwwroot/xxx.xxx.xxx/runtime/test/商品copy"
// ["nums"] => int(2)
// ["child"] => array(2) {
// [0] => array(11) {
// ["id"] => string(5) "0_4_3"
// ["pid"] => string(3) "0_4"
// ["type"] => string(4) "file"
// ["name"] => string(21) "说明文档-copy.txt"
// ["suffix"] => string(3) "txt"
// ["size"] => int(18)
// ["updatetime"] => int(1713545384)
// ["mime"] => string(0) ""
// ["path"] => string(78) "/www/wwwroot/xxx.xxx.xxx/runtime/test/商品copy/说明文档-copy.txt"
// ["nums"] => int(0)
// ["child"] => array(0) {
// }
// }
// [1] => array(11) {
// ["id"] => string(5) "0_4_4"
// ["pid"] => string(3) "0_4"
// ["type"] => string(4) "file"
// ["name"] => string(16) "说明文档.txt"
// ["suffix"] => string(3) "txt"
// ["size"] => int(18)
// ["updatetime"] => int(1713545344)
// ["mime"] => string(0) ""
// ["path"] => string(73) "/www/wwwroot/xxx.xxx.xxx/runtime/test/商品copy/说明文档.txt"
// ["nums"] => int(0)
// ["child"] => array(0) {
// }
// }
// }
// }
//}
// 删除文件
//$res = \app\common\utils\File::rm($file);
//dump($res); // 输出=>bool(true)
// 删除文件夹
//$res = \app\common\utils\File::rmDir($dir);
//dump($res); // 输出=>bool(true)
类
<?php
namespace app\common\utils;
/**
* 文件
* 安装扩展:fileinfo
*/
class File {
/**
* 读取文件
* @param $file
* @return false|string
*/
public static function read($file)
{
if (!is_file($file)) {
return false;
}
return file_get_contents($file);
}
/**
* 逐行读取到数组中
* @param $file
* @return array|bool
*/
public static function readLine($file)
{
if (!is_file($file)) {
return false;
}
$data = [];
$handle = fopen($file, "r");
while (($line = fgets($handle)) !== false) {
array_push($data,$line);
}
fclose($handle);
return $data;
}
/**
* 写入文件
* @param $file
* @param $content
* @param $append
* @return false|int
*/
public static function write($file,$content,$append = true)
{
if (!file_exists($file)) {
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
return file_put_contents($file,$content,$append ? FILE_APPEND : 0);
}
/**
* 删除文件
* @param $file
* @return bool
*/
public static function rm($file)
{
if (!is_file($file)) {
return false;
}
file_exists($file) && @unlink($file);
return true;
}
/**
* 删除文件夹
* @param string $dirname 目录
* @param bool $withself 是否删除自身
* @return boolean
*/
public static function rmDir($dirname,$withself = true)
{
if (!is_dir($dirname)) {
return false;
}
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dirname, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
if ($withself) {
@rmdir($dirname);
}
return true;
}
/**
* 复制文件
* @param string $source 源文件
* @param string $dest 目标文件
*/
public static function copy(string $source, string $dest)
{
if (!file_exists($source)) {
return false;
}
$dir = dirname($dest);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
return copy($source, $dest);
}
/**
* 复制文件夹
* @param string $source 源文件夹
* @param string $dest 目标文件夹
*/
public static function copyDir(string $source, string $dest)
{
if (!is_dir($source)) {
return false;
}
if (!is_dir($dest)) {
mkdir(dirname($dest), 0755, true);
}
foreach (
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
) as $item
) {
if ($item->isDir()) {
$sontDir = $dest . DS . $iterator->getSubPathName();
if (!is_dir($sontDir)) {
mkdir($sontDir, 0755, true);
}
} else {
copy($item, $dest . DS . $iterator->getSubPathName());
}
}
}
/**
* 树形结构列出文件夹底下所有文件和文件夹
* @param $dir
* @param string $pid
* @return array
*/
public static function tree($dir,$pid = '0')
{
$result = [];
$items = scandir($dir);
foreach ($items as $key=>$item) {
if ($item == '.' || $item == '..') continue;
$path = $dir . DIRECTORY_SEPARATOR . $item;
$id = $pid.'_'.($key+1);
if (is_dir($path)) {
$child = self::tree($path,$id);
$result[] = [
'id'=>$id,
'pid'=>$pid,
'type'=>'dir',
'name'=>$item,
'suffix'=>'',
'size'=>self::dirsize($path),
'updatetime'=>filemtime($path),
'mime'=>'',
'path'=>$path,
'nums'=>count($child),
'child'=>$child
];
} else {
$fileinfo = self::fileinfo($path);
$result[] = [
'id'=>$id,
'pid'=>$pid,
'type'=>'file',
'name'=>$item,
'suffix'=>$fileinfo['extension'],
'size'=>$fileinfo['size'],
'updatetime'=>$fileinfo['updatetime'],
'mime'=>$fileinfo['mime'],
'path'=>$path,
'nums'=>0,
'child'=>[]
];
}
}
return $result;
}
/**
* 判断文件或文件夹是否可写
* @param string $path 文件或文件夹
* @return bool
*/
public static function isCanWrite(string $path)
{
if (DIRECTORY_SEPARATOR === '/') {
return is_writable($path);
}
if (is_dir($path)) {
$file = rtrim($path, '/') . '/' . md5(mt_rand());
if (($fp = @fopen($file, 'ab')) === false) {
return false;
}
fclose($fp);
@chmod($file, 0777);
@unlink($file);
return true;
} elseif (!is_file($path) or ($fp = @fopen($path, 'ab')) === false) {
return false;
}
fclose($fp);
return true;
}
/**
* 文件夹大小
* @param $dir
* @return false|int|mixed 字节
*/
public static function dirsize($dir)
{
$size = 0;
$dir_iterator = new \DirectoryIterator($dir);
foreach ($dir_iterator as $file) {
if ($file->isDot()) continue;
if ($file->isDir()) {
$size += self::dirsize($file->getPathname());
} else {
$size += filesize($file->getPathname());
}
}
return $size;
}
/**
* 获取文件信息
* @param $file
* @return array
*/
public static function fileinfo($file)
{
if (!is_file($file)) {
return [];
}
$info = pathinfo($file);
if (self::isImage($file)) {
$image = getimagesize($file);
$width = $image[0];
$height = $image[0];
$mime = $image['mime'];
}
else {
$width = 0;
$height = 0;
$mime = '';
// 获取mime
// 检查Fileinfo扩展是否已经安装并可用
if (extension_loaded('fileinfo')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file);
finfo_close($finfo);
}
}
return [
// 获取文件名,示例:sql-log.txt
'basename'=>$info['basename'],
// 获取文件名,示例:sql-log
'filename'=>$info['filename'],
// 获取文件扩展名,示例:txt
'extension'=>$info['extension'],
// 获取文件目录,示例:/www/wwwroot/xxx.xxx.xx/runtime/log
'dirname'=>$info['dirname'],
// 获取文件的最后修改时间,示例:1713529993
'updatetime'=>filemtime($file),
// 获取文件大小(字节数Byte),示例:68425
'size'=>filesize($file),
// 获取图片宽,示例:200
'width'=>$width,
// 获取图片高,示例:180
'height'=>$height,
// 获取文件mime,示例:image/png
'mime'=>$mime,
];
}
/**
* 文件是否图片
* @param $file
* @return bool
*/
public static function isImage($file)
{
if (!is_file($file)) {
return false;
}
$imageInfo = getimagesize($file);
if (empty($imageInfo)) {
return false;
}
return in_array($imageInfo[2], [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_WEBP]);
}
}
栏目分类全部>