<?php
header("content-type:text/html; charset=utf-8");
/**
* php文件操作!
* 2012-01-16
**/
echo "<form name=fileForm id=fileForm method=post action='' enctype=multipart/form-data>";
$wrap = '<br />'; //win下全局换行
$ds = DIRECTORY_SEPARATOR;//全局分隔符。
$post = isset($_POST) ? $_POST : '';
/* PHP读取的文件类型:
* win:file、dir、unknown三种
* unix:block、char、dir、fifo、file、link、unknown七种
*/
echo '<h3>----------------------------文件类型---------------------------------------</h3>';
//使用filetype()函数来分别获取win和unix系统下的某文件类型 这里主要是win系统。(电脑是win7系统)
echo 'filetype("D:/")函数:'.filetype("D:/"); //cout>> dir; 这是一个目录类型
echo $wrap;
echo 'filetype("E:/FILE/TEMP/9.jpg")函数:'.filetype("E:/FILE/TEMP/9.jpg"); //cout>> file; 这是win下的一个普通file文件类型
//echo ' - '.filetype("E:/FILE/个人源代码/"); //他无法识别中文目录
echo ' - '.filetype("E:/FILE/TEMP/index.php"); //count>> file; 同样也是一个file文件
echo $wrap;
//衍生
//is_file() 判断给定的文件是否是一个正常的文件
echo 'is_file()函数:'.is_file("E:/FILE/TEMP/9.jpg");
echo $wrap;
//is_dir() 判断给定的文件名是否是一个目录
echo 'is_dir()函数:'.is_dir("E:/");
echo $wrap;
echo '<h3>----------------------------文件属性---------------------------------------</h3>';
//获取文件属性
//函数集:file_exists()、fielsize()、is_readable()、is_writable()、is_executable()、filectime()、filemtime()、fileatime()、stat()
/**
* 获取文件属性
*/
echo 'EXP:'.$file = 'E:/FILE/TEMP/CSDN tup云技术.txt';
//getFileAttribute($file);
function getFileAttribute($file)
{
global $wrap;
if(!file_exists($file))
{
echo '该文件或目录不存在!';
return '';
}
echo '获取[<b style=color:red;>'.$file.'</b>]文件属性:'.$wrap;
echo '判断是目录还是文件:';
if(is_file($file))
{
echo '这是一个文件'.$wrap;
}
if(is_dir($file))
{
echo '这是一个目录'.$wrap;
}
echo '文件类型:'.filetype($file).$wrap;
echo '判断文件可写is_writable() => ';
$write = is_writable($file);
echo $write ? '<b style=color:green;>可写</b>' : '<b style=color:red;>不可写</b>';
echo $wrap;
echo '判断文件可读is_readable() => ';
$read = is_readable($file);
echo $read ? '<b style=color:green;>可读</b>' : '<b style=color:red;>不可读</b>';
echo $wrap;
echo '判断文件可读is_executable() => ';
$execut = is_executable($file);
echo $execut ? '<b style=color:green;>可执行</b>' : '<b style=color:red;>不可执行</b>';
echo $wrap;
echo '获取文件创建时间filectime() 返回一个UNIX时间戳格式哦 => ';
echo date("Y-m-d H:i:s",filectime($file));
echo $wrap;
echo '获取文件最新修改时间filemtime() 返回一个UNIX时间戳格式哦 => ';
echo date("Y-m-d H:i:s",filemtime($file));
echo $wrap;
echo '获取文件最近访问时间fileatime() 返回一个UNIX时间戳格式哦 => ';
echo date("Y-m-d H:i:s",fileatime($file));
echo $wrap;
echo '获取文件的大小filesize()返回字节数哦,亲! => ';
echo getFileSize(filesize($file));
echo $wrap;
echo '获取文件的其他属性stat() 返回一个大数组,也就是上边那些的大集合 => array(';
$statArr = stat($file);
echo $wrap;
if(is_array($statArr) && !empty($statArr))
{
$i = 0;
foreach($statArr as $key => $value)
{
if(!is_numeric($key))
{
echo $i.'=['.$key.']'.' => '.$value.$wrap;
$i++;
}
}
}
echo ')'.$wrap;
echo '最后清除缓存 clearstatcache() 然后没了';
clearstatcache();
echo $wrap;
}
echo '<hr />';
echo '<h4>上传一个文件,查看他的属性!</h4>';
echo '<input type=file name=upload >';
echo '<input type=submit value=上传文件查看查看属性 />';
echo '<input type=hidden name=upF value=f />';
if(isset($post['upF']) && isset($_FILES['upload']))
{
$upload = $_FILES['upload'];
echo $wrap;
echo '原始文件名:[<b style=color:blue>'.$upload['name'].'</b>] 上传后的临时存放路径:[<b style=color:red;>'.$upload['tmp_name'].'</b>]<br />';
getFileAttribute($upload['tmp_name']);
}
echo '<h3>----------------------------目录操作---------------------------------------</h3>';
//---------------------目录部分---------------------------
$path = 'e:'.$ds.'xampp'.$ds.'htdocs'.$ds.'www'.$ds.'classStudy'.$ds.'phpFile.php';
/*
* 目录路劲属性
*/
'basename() 返回路径中的文件名:'.basename($path).$wrap;
'basename(str,params) 不返回文件的后缀名:'.basename($path,'.php').$wrap; //params可选参数:只要原始路径中包含可选参数的字符,那么包含的那部分将不会返回
'dirname() 返回当前路径中去掉文件名的路径:'.dirname($path).$wrap;
'pathinfo() 返回一个关联数组:目录名、文件名、文件后缀名'.$wrap;
$pathArr = pathinfo($path);
"pathArr['dirname'] = ".$pathArr['dirname'].'(返回当前路径目录)'.$wrap;
"pathArr['basename'] = ".$pathArr['basename'].'(返回当前路径文件名)'.$wrap;
"pathArr['extension'] = ".$pathArr['extension'].'(返回当前路径文件的后缀名)'.$wrap;
//--------------------遍历目录---------------------------
//opendir() 打开一个目录
//readdir() 读取已打开目录里的东西
//closedir() 关闭已打开的目录
//rewinddir() 将打开后的正在读取目录指针返回到开头
//写一个函数返回当前目录下的所有东西,以及子目录、文件的个数
echo '<hr />';
echo '<h4>输入一个目录,看看他下边有些啥!</h4>';
echo 'EXP:'.$dirPath = $path = 'e:'.$ds.'xampp'.$ds.'phpMyAdmin';
echo $wrap;
echo '<input type=text name=dirstr >';
echo '<input type=submit value=点击查看输入的目录的信息 />';
//echo '<input type=hidden name=dirF value=f />';
if(isset($post['dirstr']))
{
$dirPath = $_POST['dirstr'];
//viewFolder($dirPath);
}
function viewFolder($dirPath)
{
global $wrap;
global $ds;
echo '<table width=100% border=0 cellspacing=0 cellpadding=0 style="border:#318ba5 1px solid; font-size:12px;font-family:Courier New,"幼圆";">';
if(!is_dir($dirPath))
{
echo '<tr><td align=center><h3 style=color:red>这不是一个有效的目录</h3></td></tr></table>';
return;
}
$dirInfo = opendir($dirPath);
while($row = readdir($dirInfo))
{
$folderArr[] = $row;
}
closedir($dirInfo);
if(is_array($folderArr) && !empty($folderArr))
{
$di = 0;
$fi = 0;
$dirSize = 0;
echo '<tr bgcolor=#348aa5>';
echo '<td colspan=6 align=center ><h3 style=color:#fff;>['.$dirPath.']当前目录下的信息</h3></td></tr>';
echo '<tr style="height:30px; background:#cae8ea; font-weight:bold; font-size:16px;"><td>文件列标</td><td>文件名</td><td>文件大小</td><td>创建时间</td><td>是否可写</td><td>是否可读</td></tr>';
foreach($folderArr as $key => $value)
{
if($value!='.' && $value!='..')
{
$bgcolor = ($key%2==0 ? '#f5fafa' : '#ebf9d6');
$truePath = $dirPath.$ds.$value;
if(is_dir($truePath))
{
$tempSize = whileDir($truePath);
echo '<tr bgcolor='.$bgcolor.' ><td>'.$key.'</td><td><b style=color:red;>'.$value.'</b></td><td colspan=4>'.getFileSize($tempSize).'</td></tr>';
$dirSize +=$tempSize;
$di++;
}
elseif(is_file($truePath))
{
echo '<tr bgcolor='.$bgcolor.'><td>'.$key.'</td>';
echo '<td>'.$value.'</td>';
echo '<td>'.getFileSize(filesize($truePath)).'</td>';
echo '<td>'.date("Y-m-d H:i:s",filectime($truePath)).'</td>';
echo '<td>'.(is_writable($truePath) ? '<span style=color:green;>√</span>' : '<span style=color:red;>×</span>').'</td>';
echo '<td>'.(is_readable($truePath) ? '<span style=color:green;>√</span>' : '<span style=color:red;>×</span>').'</td></tr>';
$dirSize += filesize($truePath);
$fi++;
}
else
{
echo '<tr bgcolor='.$bgcolor.'><td>'.$key.'</td><td colspan=6>不是文件和目录</td></tr>';
}
}
}
echo '<tr bgcolor=#348aa5><td colspan=6 align=center colspan=6 align=center ><h3 style=color:#fff;>该目录下的子目录一共有:['.$di.']个 文件共有:['.$fi.']个 总大小:['.getFileSize($dirSize).']</h3></td></tr>';
}
else
{ echo '<tr><td><h3 style=color:red>这不是一个有效的目录</h3></td></tr>'; }
echo '</table>';
}
/**
* 计算目录的大小。递归函数。接受一个打开了的文件句柄做为参数
*/
function whileDir($dirInfo)
{
global $ds;
$size = 0;
$openFile = opendir($dirInfo);
while($row = readdir($openFile))
{
if($row!='.' && $row!='..')
{
$subFile = $dirInfo.$ds.$row;
if(is_dir($subFile))
{ $size += whileDir($subFile); }
if(is_file($subFile))
{ $size += filesize($subFile); }
}
}
closedir($openFile);
return $size;
}
echo $wrap;
//磁盘的可用空间和总空间
echo 'disk_free_space() 当前F磁盘区间的可用大小:'.getFileSize(disk_free_space("F:/")).$wrap;
echo 'disk_total_space() 当前D磁盘区间的总大小:'.getFileSize(disk_total_space("D:/")).$wrap;
echo '--------------------------------<br />';
//建立和删除目录
//mkdir() 在任何地方建立一个目录
$expDirName = '468';
/**
* 简单的建立一个目录,然后删除这个目录
* mkdir()只能在当前已存在的目录下建立新的目录!!!!!!!!!!!!!!
* '目录名:'.$expDirName.$wrap;
* '使用mkdir()函数在当前目录下建立新目录:'.mkdir($expDirName).$wrap;
* viewFolder($expDirName);
* '使用rmdir()函数删除刚才新建的目录:'.rmdir($expDirName).'<span color=red;>该函数只能用于非空、并且物理存在的目录!</span>'.$wrap;
**/
echo '<hr />';
echo '<h4>输入一个目录,然后删除它!</h4>';
echo '<input type=text name=delDir >';
echo '<input type=submit value=点击删除输入的目录 />';
if(isset($post['delDir']))
{
$dirPath = $_POST['delDir'];
echo $wrap;
//delDir($dirPath);
}
/**
* 定义一个自定义删除当前目录的函数 delDir($dirPath)
* 递归调用函数
* 该目录必须存在!
* 该方法会删除当前目录下的所有文件!谨慎使用!!!!!!!!!
**/
function delDir($dirPath)
{
global $wrap;
global $ds;
$reInfo = false;
if(file_exists($dirPath))
{
$openDir =@opendir($dirPath);
if($openDir)
{
while($row = readdir($openDir))
{
if($row!='.' && $row!='..')
{
echo $subPath = $dirPath.$ds.$row;
if(is_dir($subPath))
{
$ddir = delDir($subPath);
echo $ddir==1 ? '成功删除该目录:'.$subPath.$reInfo = true : '该目录删除失败:'.$subPath.$reInfo = false;
echo $wrap;
}
elseif(is_file($subPath))
{
$dfile = unlink($subPath);
echo $dfile==1 ? '成功删除该文件:'.$subPath.$reInfo = true : '该文件删除失败:'.$subPath.$reInfo = false;
echo $wrap;
}
else
{ echo ' 这不是有效的文件或目录!'; $reInfo = false; }
}
}
rmdir($dirPath);
closedir($openDir);
}
else
{ echo $dirPath.' 该目录打开失败!'; $reInfo = false; }
}
else
{ echo $dirPath.' 该目录不存在!'; $reInfo = false; }
return $reInfo;
}
//---------------------------复制和移动文件目录-----------------
/**
* 使用PHP函数:copy() => 只能单一复制某个文件。 要想复制整个文件目录须配合PHP函数mkdir()使用
* 移动文件目录与复制目录为同一原理,只需要在复制完后删除掉原目录即可!
* 下面是自定义复制目录!
*/
echo '<hr />';
echo '<h4>输入一个目录,然后复制它到指定的目录里!</h4>';
echo 'EXP:E:\aa 到 F:\bb'.$wrap;
echo '复制 <input style=width:300px; type=text name=csDir >';
echo ' 到 <input style=width:300px; type=text name=ctDir >';
echo '<input type=submit value=点击复制目录 />';
if(isset($post['csDir']) && isset($post['ctDir']))
{
$csPath = $post['csDir'];
$ctPath = $post['ctDir'];
echo $wrap;
copyDir($csPath,$ctPath);
echo '复制过去后的文件目录:<br />';
viewFolder($ctPath);
}
//自定义复制目录方法
function copyDir($csPath,$ctPath)
{
global $ds;
if(is_file($ctPath))
{
echo '目标路径是一个文件!';
return;
}
if(!file_exists($ctPath))
{ mkdir($ctPath); }
$openDir = opendir($csPath);
if($openDir)
{
while($name = readdir($openDir))
{
if($name!='.' && $name!='..')
{
$subSourcePath = $csPath.$ds.$name;
$subTargetPath = $ctPath.$ds.$name;
//如果这里是一个目录,那么再次调用该函数!
if(is_dir($subSourcePath))
{ copyDir($subSourcePath,$subTargetPath); }
if(is_file($subSourcePath))
{ echo '这是一个文件<br />'.$subSourcePath; copy($subSourcePath,$subTargetPath); }
}
}
closedir($openDir);
}
}
echo '<h3>----------------------------文件操作---------------------------------------</h3>';
//读取文件 fopen()打开文件、fclose()关闭已打开文件
//fopen('文件路径','打开模式') :打开模式参数:r -> 只写读模式打开
// r+ -> 读写模式打开
// w -> 写入模式打开 如果存在该文件,那么他会删除源文件里的所有内容,并重新写入新内容。如果不存在则尝试创建新文件!
// w+ -> 读写模式打开
// x -> 创建并以写入方式 如果文件已存在则返回FALSE,并给出Error错误信息!
// x+ -> 创建并以读写方式打开。
// a -> 写入方式打开,并将文件指针指向文件末尾! 新数据也将在文件末尾插入
// a+ -> 读写方式打开
// b -> 以二进制方式打开文件 用于与其他模式连接 windows能区分二进制,UNIX则 不区分
// t -> 以文本方式打开文件。只适用与windows
//写入文件 fwrite()/fputs()[别名函数] 快速函数file_put_contents()集成fopen()->fwrite()->fclose()函数功能!
$handle = 'fileOper.txt';
//if(is_file($handle)) { unlink($handle); }
$fopen = fopen($handle, 'r');
if($fopen)
{
echo '以r模式 打开',$handle,'文件成功!'.$wrap;
/*echo '现在写入一些内容进去:十个新年快乐!'.$wrap;
for($i=0 ; $i<=10 ; $i++)
{
fwrite($fopen, $i."新年快乐 a模式!;\n");
}*/
//读取函数 fread()、file_get_contents()将文件读入字符串[类似快速写入函数] fgets()打开文件一行 fgetc()打开文件的字符 readfile()将读取的文件输出到输出缓冲 feof()判断是否到了文件末尾
/*$content = fread($fopen,filesize($handle));
echo $content.$wrap;*/
//feof()循环读取文件中的内容
/*while(!feof($fopen))
{ fread($fopen, 26); }*/
//fgets()获取文件内容 配合feof()函数使用 默认每一行的长度为1024
/*while (!feof($fopen))
{ echo fgets($fopen),$wrap; }*/
//fgetc()获取 一个字符一个字符的获取!
/*while (false!==($char=fgetc($fopen)))
{ echo $char.$wrap; }*/
fclose($fopen);
}
else
{
echo '文件打开失败!'.$wrap;
}
//file()将读取的文件输出到一个数组中 这个好使!
/*$array = array();
$array = file($handle);*/
//使用file_get_contents()直接读取!脱离fopen->fread()->fclose()函数!
/*file_get_contents($handle);*/
//远程连接文件
$url = 'http://www.gamewave.net/';
var_array(file($url));
/*$handle = fopen($url, 'r') or die($url.'远程打开失败!');
$conArr = array();
echo fread($handle, 9000);*/
//---------------------公共函数部分---------------------------
echo '</form>';
/**
* 处理获取到的文件的size大小。
* 直接拿的细说PHP书上的。
*/
function getFileSize($size)
{
$return = '';
$suffix = '';
if($size >= pow(2,40))
{
$return = round($size/pow(2,40),2);
$suffix = 'TB';
}
elseif($size >=pow(2,30))
{
$return = round($size/pow(2,30),2);
$suffix = 'GB';
}
elseif($size >= pow(2,20))
{
$return = round($size/pow(2,20),2);
$suffix = 'MB';
}
elseif($size >= pow(2,10))
{
$return = round($size/pow(2,10),2);
$suffix = 'KB';
}
else
{
$return = $size;
$suffix = 'Byte';
}
return $return.' '.$suffix;
}
/**
* 判断输入的字符串是目录还是文件类型
*/
function getFileType($str)
{
$type = filetype($str);
switch ($type)
{
case 'block':
return '这是一个磁盘分区';
case 'char':
return '这是I/O字符输入输出设备';
break;
case 'dir':
return '这是一个目录类型';
break;
case 'fifo':
return '这是一个命名管道';
break;
case 'file':
return '这是一个文件类型';
break;
case 'link':
return '这是一个符号连接 类似win的快捷方式';
break;
case 'unknown':
return '这是一个位置的类型';
break;
default :
return '这什么都不是。路径错了!';
}
}
//格式化数组格式
function var_array($array)
{
echo '<pre>';
var_dump($array);
echo '</pre>';
}
?>
PHP基础->文件读写->遍历目录->文件属性->建立、删除文件目录
最新推荐文章于 2021-04-29 15:41:05 发布