您的位置:首页 > 编程语言 > PHP开发

phpfile文件系统基础

2017-01-10 23:22 232 查看
---------------------------------file基础1----
<?php
//获取文件类型
//$filename = "test.txt";
$filename = "test";
echo filetype($filename);
--------------------------判断是文件还是目录--
<?php
header("Content-Type:text/html;charset=utf-8");
//判断是文件还是目录
//路径要写对,不然不能找不到文件无法判断
//$filename = "test.txt";
$filename = "./test/1.txt";
if(is_file($filename)){
echo "是文件";
}
if(is_dir($filename)){
echo "是目录";
}
-------------------------获取文件的大小------
<?php
//获取文件的大小
$filename = "history.html";
echo getFileSize($filename);
function getFileSize($filename){
$size = filesize($filename); //bytes
$unit = "";
if($size>=pow(2,40)){
$size = $size/pow(2,40);
$unit = "TB";
}else if($size>=pow(2,30)){
$size = $size/pow(2,30);
$unit = "GB";
}else if($size>=pow(2,20)){
$size = $size/pow(2,20);
$unit = "MB";
}else if($size>=pow(2,10)){
$size = $size/pow(2,10);
$unit = "KB";
}else{
$unit = "bytes";
}
return $size.$unit;
}
-------------获取文件的创建+修改+访问时间------
<?php
header("Content-Type:text/html;charset=utf-8");
//获取文件的创建时间
$filename = "test.txt";
//date("",)
echo "文件的创建时间:".date("Y-m-d H:i:s",filectime($filename));
echo "<br/>";
echo "文件的修改时间:".date("Y-m-d H:i:s",filemtime($filename));
echo "<br/>";
echo "文件的访问时间:".date("Y-m-d H:i:s",fileatime($filename));
echo "<br/>";
--------------------------判断文件是否存在------
<?php
header("Content-Type:text/html;charset=utf-8");
// 判断文件是否存在,也能够判断目录是否存在
$filename = "test1";
if(file_exists($filename)){
echo "存在";
}else{
echo "不存在";
}
----------------------------目录的遍历-----------
<?php
//目录的遍历
$filenames = glob("*.php");
foreach($filenames as $file){
echo $file;
echo "<br/>";
}
-------------------------PHP对于目录的操作-------
相对目录和绝对目录
相对目录:不是从根目录计算起的目录都叫相对目录。
在程序中:.  代表当前目录
.. 上一级目录
绝对目录:从根目录计算起的目录就叫绝对目录
windows操作系统的根目录:盘符
linux操作系统的根目录:/
------试着用opendir打开目录循/环读取出目录下的内容--
<?php
//遍历目录
//1、打开目录
$pathname = "test";
/*
* 3、试着用opendir打开目录
循环读取出目录下的内容。
*/
$dir = opendir($pathname);
//读取目录下的内容
echo readdir($dir); //.当前目录
echo "<br/>";
echo readdir($dir);//..上一级目录
echo "<br/>";
echo readdir($dir);//a 当前目录下的文件内容,读取的内容是不固定的
echo "<br/>";
echo readdir($dir);
echo "<hr/>";
var_dump(readdir($dir));
//释放目录资源
closedir($dir);
-----------用opendir打开目录,循环读取出目录下的内容-----
<?php
header("Content-Type:text/html;charset=utf-8");
/*3、试着用opendir打开目录
环读取出目录下的内容。
*/
$pathname='test';
//打开目录
$dir=opendir($pathname);
//先读取一次目录
$filename=readdir($dir);//.
//循环读取目录下的内容
while($filename){//.一个点永远是true有内容的
//先打印第一次读取的目录
echo $filename.'<br/>';
//.
//..
//1.txt
//a
//再读取目录下的内容
$filename=readdir($dir);//写这条语句用来控制循环结束,会一直读到false
//..
//1.txt
//a
//false
}
//关闭资源
closedir($dir);
--------------------//过滤掉 . 和 ..--------
<?php
header("Content-Type:text/html;charset=utf-8");
/*3、试着用opendir打开目录
环读取出目录下的内容。
*/
$pathname='test';
//打开目录
$dir=opendir($pathname);
//先读取一次目录

//循环读取目录下的内容
while($filename=readdir($dir)){
//过滤掉 . 和 ..
if($filename!='.'&&$filename!='..'){
echo $filename.'<br/>';
}
}
//关闭资源
closedir($dir);
--------------file基础_2----------------
<?php
header("Content-Type:text/html;charset=utf-8");

/*
* 4、作业:
对目录中的内容进行判断
文件还是目录
若是文件,则输出:文件:文件名称
若是目录,则输出:目录:目录名称
*/
//1、打开目录
$pathname = "test";
$dir = opendir($pathname);
//循环读取目录下的内容
while($filename = readdir($dir)){
//过滤掉"."和".."
if($filename!="."&&$filename!=".."){
//拼装路径,因为不拼接路径,程序无法辨别正确的正确的路径
$filename = $pathname."/".$filename;
echo $filename."<br/>";
if(is_dir($filename)){
//不拼接路径,这里is_dir($filename)全都是false
echo "目录:".$filename."<br/>";
}

if(is_file($filename)){
echo "文件:".$filename."<br/>";
}
echo "<hr/>";
}
}
//3、释放资源
closedir($dir);
?>
---------统计目录下的文件和目录数(使用递归找到当前文件夹的所有子文件)-----
<?php
header("Content-Type:text/html;charset=utf-8");

//统计目录下的文件和目录数
//目录计数
$dir_num = 0;
//文件计数
$file_num = 0;
$pathname = "test";
countDir($pathname);
echo "目录数:".$dir_num;
echo "<br/>";
echo "文件数:".$file_num;
function countDir($pathname){
global $dir_num;
global $file_num;

//1、打开目录
$dir = opendir($pathname);
//循环读取目录下的内容
while($filename=readdir($dir)){
//过滤掉“.”和“..”
if(!($filename=="."||$filename=="..")){
//判断读取到的内容是目录还是文件
//拼装路径
$filename = $pathname."/".$filename;
if(is_dir($filename)){
//是目录,则有可能次目录下存在
//子目录或者文件
//test/a
countDir($filename);
//给目录计数加1
$dir_num++;
}else{
//是文件
$file_num++;
}
}
}
//3、释放资源
closedir($dir);
}

//定义函数countDir2,用来统计test/a
function countDir2($pathname){//用之前的$filename来作为当前函数的$pathname
//统计pathname 即:test/a下的文件和目录数
global $dir_num;
global $file_num;

//打开目录
$dir = opendir($pathname);
//循环读取目录下的内容并统计
while($filename = readdir($dir)){
//过滤掉"."和".."
if(!($filename=="."||$filename=="..")){
//拼装目录
$filename = $pathname."/".$filename;
if(is_dir($filename)){
//若是目录
$dir_num++;
}else{
//若是文件
$file_num++;
}
}
}
//释放资源
closedir($dir);
}
-------统计目录下的文件的大小(使用递归找到当前文件夹的所有子文件大小)----
<?php
//2、统计目录的大小。
$pathname = "test";
echo sumDir($pathname);
function sumDir($pathname){
//定义变量,记录目录下文件的大小
$size = 0;
//打开目录
$dir = opendir($pathname);
//循环读取目录下的内容,累加记录目录下文件的大小
while($filename=readdir($dir)){
//过滤掉"."和".."
if($filename!="."&&$filename!=".."){
//拼装路径
$filename = $pathname."/".$filename;
//filename = "test/a"
//filename = test/1.txt
//filename = test/2.txt
if(is_dir($filename)){
//如果是目录,则不用统计大小
//目录没有的大小,是文件的累加
// test/a/3.txt
$size+=sumDir($filename);
//$size+=sumDir2($filename);调用一个新的方法
}else{
//如果是文件
//$size = $size+filesize(test/1.txt);
//$size = 6161bytes
//$size = 6161+filesize(test/2.txt)
$size+=filesize($filename);
}
}
}
//释放资源,先关掉资源在return,不然return后面就不执行了
closedir($dir);
return $size;
}

function sumDir2($pathname){
//统计文件大小的变量
$size = 0;
//$pathname test/a
//打开目录
$dir = opendir($pathname);
//循环读取并统计目录下文件的大小
while($filename=readdir($dir)){
//过滤掉"."和".."
if($filename!="."&&$filename!=".."){
//拼装目录
$filename = $pathname."/".$filename;
if(is_dir($filename)){
//是目录
}else{
//是文件 3.txt
$size+=filesize($filename);
}
}

}
//释放资源
closedir($dir);
return $size;
}
//每次递归调用函数的时候,又重新开辟了一段内存
//变量又是全新的变量,不是之前的那个了,
--------------------创建目录-----------------------
<?php
//创建目录
//相对路径
$pathname = "test_new";
//绝对路径
$pathname = "D:/www/psd1608/07_FILE/day03";
var_dump(mkdir($pathname));
---------------------重命名-目录--------------------
<?php
//重命名,旧的目录不管里面是否有内容都会重命名
//$oldpathname = "test_new";
$oldpathname = "test.txt";
//$newpathname = "test1";
$newpathname = "test1.txt";
var_dump(rename($oldpathname,$newpathname));
---------------------重命名-文件--------------------
<?php
header('Content-Type:text/html;charset=utf-8');
//重命名对于文件也是有效的
$oldpathname='test/new.txt';
$newpathnew='test/1.txt';
var_dump(rename($oldpathname,$newpathnew));
//copy只对文件有效
---------------------目录的复制-------------------
<?php
header("Content-Type:text/html;charset=utf-8");
//目录的复制
$oldpathname = "test";
$newpathname = "test3";
copyDir($oldpathname,$newpathname);
function copyDir($oldpathname,$newpathname){
//判断newpathname是否存在
if(file_exists($newpathname)){
//若存在,对$newpathname进行判断,若
//newpathname为文件,则无法进行拷贝
if(is_file($newpathname)){
echo "目标为文件,无法拷贝";
exit;
}

}else{
//不存在,则创建newpathname
mkdir($newpathname);
}
//打开目录
$dir = opendir($oldpathname);
//循环读取oldpathname下的内容,并进行拷贝
while($filename = readdir($dir)){
//过滤掉“.”和“..”
if($filename!="."&&$filename!=".."){
//$filename = "2.txt"
$s_filename = $oldpathname."/".$filename;
//$s_filename = test/2.txt
$d_filename = $newpathname."/".$filename;
//$d_filename = test3/2.txt
//判断是否是目录
if(is_dir($s_filename)) {
//是目录,暂时不管
//递归执行
//$s_filename = test/a
//$d_filename = test3/a
copyDir($s_filename,$d_filename);
}else{
//是文件,则进行拷贝
copy($s_filename,$d_filename);
//copy("test/1.txt","test3/1.txt");
//copy("test/2.txt","test3/2.txt");
}

}
}
//释放资源
closedir($dir);
}
------------文件目录的删除-rmdir($filename)----------
<?php
//文件目录的删除
//1、在当前目录下有空目录test,试着用php函数删除该目录。
$pathname = "test";
var_dump(rmdir($pathname));
------------file基础3----------------
<?php
//文件创建函数
$filename = "test.php";
$filename = "test";
$filename = "a.q";
$filename = "D:/a";
//在window下权限很大,只要有权限,touch喜欢创建什么文件都行
var_dump(touch($filename));
-------------文件写入(file_put_contents)-------------
<?php
//文件写入应用举例
//$filename = "test.txt";
//也可以修改远程地址,但是要有权限才行,一般用在本地地址
$filename = "test1.txt";
var_dump(file_put_contents($filename,"have a nice day"));
--------------fopen mode的说明------------------------
<?php
//fopen mode的说明
/*
* 1、原有文件test.txt,有内容 123,读取出原有文
* 件中的一个字符。在原有文件末尾追加字符串hello.
*/
//1、打开文件
$filename = "test.txt";
$filename = "test5.txt";
$fh = fopen($filename,"a+");
//2、文件的具体操作
//读取文件中的内容
echo fgetc($fh);
echo "<hr/>";
//写入文件内容
var_dump(fwrite($fh,"abcd"));
//3、释放资源
fclose($fh);
-------------fgets应用举例 循环读取出文件的内容---
<?php
//fgets应用举例 循环读取出文件的内容
//1、打开文件
$fh = fopen("test.txt","r");
//2、操作
while($contents = fgets($fh)){
echo $contents;
echo "<br/>";
}
//3、释放资源
fclose($fh);
-------------fread($fh,filesize($filename)----------
<?php
//fread 应用举例
/*
* 2、读取文件 test.txt中的内容,要求一次指定的长度正
* 好能读取出文件的全部内容。
*/
//1、打开文件
$filename = "a.txt";
$fh = fopen($filename,"r");
//2、操作
echo fread($fh,filesize($filename));
//长度可以写大一些,也是可以的,如100

//3、释放资源
fclose($fh);
----------------------------------------------------
<?php
//fread 应用举例
/*
* 2、读取文件 test.txt中的内容,要求一次指定的长度正
* 好能读取出文件的全部内容。
*/
//1、打开文件
$filename = "test.txt";
$fh = fopen($filename,"r");
//2、操作
echo fread($fh,filesize($filename));

//3、释放资源
fclose($fh);
----------------------------------------------------
<?php
//feof 应用举例
//1、打开文件
$filename = "test.txt";
$fh = fopen($filename,"r");
//2、操作
while(!feof($fh)){
echo fgetc($fh);
}
//3、释放资源
fclose($fh);
----------------------------------------------------
<?php
//feof 应用举例
/*
* 3、应用feof判断是否到达文件尾,使用fgets
* 循环读取出test.txt文件中的内容。
*/
//1、打开文件
$filename = 'test.txt';
$fh = fopen($filename,"r");
//2、操作
//feof到文件尾 true ,没到文件尾!false = true

while(!feof($fh)){
echo fgets($fh);
}
//3、释放资源
fclose($fh);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件系统