php笔记之常用文件操作
复制代码 代码如下:
//常用文件操作函数
//第一部分 文件读写 与创建 删除 重命名等
//在开始前操作文件前 我们先判断一下是否是个文件 文件是否可执行 可读 可写
$file="test.txt";
if(file_exists($file))//盘断文件是否存在
{
echo "文件存在
";
}else
{
echo "文件不存在,已创建";
$fp=fopen($file,"w");//只读模式创建
fclose($fp);
}
if(is_file($file))
{
echo "是文件
";
}
if(is_dir($file))
{
echo "是目录
";
}
if(is_executable($file))
{
echo "文件可执行
";
}
if(is_readable($file))
{
echo "文件可读
";
}
if(is_writable($file))
{
echo "文件可写
";
}
chmod($file,0777);//完全权限
//模式说明 数字 1 表示使文件可执行,数字 2 表示使文件可写,数字 4 表示使文件可读--模式相加代表权限
$fp=fopen("test.txt","a+");//用追加读写的方式打开
//打开远程文件时
//$fp=fopen("test.txt","a+b");记得加b;
$content=fread($fp,70);//读取70字节
echo "1.{$content}
";//输出
fwrite($fp,"我是荐礼啦asdddddddddddddddddddddddddddddddddxxxxxxxxx");//已追加方式写入
$content=file_get_contents("test.txt");//读取文件 读取远程文件推荐用此函数
//$content=file_get_contents("http://www.jianlila.com");
echo "2.{$content}
";
file_put_contents("test.txt","我是爱我爸妈asdddddddddddddddddddddddddddddddddxxxxxxxxx");
//输出到文件
fclose($fp);//关闭文件句柄
$fp=fopen("test.txt","a+");
$content=fread($fp,filesize("test.txt"));
//读取全部内容 filesize($file)//文件字节数
echo "3.{$content}
";
$fp=fopen("test.txt","r");
echo "一个字符".fgetc($fp)."
";//读取一个字符
$fp=fopen("test.txt","r");
echo "一行".fgets($fp)."
";//读取一行字符
$fp=fopen("test.txt","r");
echo "剩余数据";
fpassthru($fp);
echo "
";//输出剩余数据 可以用来输出二进制文件
copy("test.txt","荐礼啦.txt");
//文件拷贝
if(file_exists("爱我爸妈.txt"))
{
unlink("爱我爸妈.txt");
//如果存在则删除文件
}
rename("荐礼啦.txt","爱我爸妈.txt");
//文件重命名
if(file_exists("荐礼啦"))
{
rmdir("荐礼啦");//删除文件夹
}else
{
mkdir("荐礼啦");//创建文件夹
}
//获取文件信息 函数
$file="test.txt";
echo "文件大小".filesize($file)."字节
";
echo "文件类型".filetype($file)."
";
//这里的文件类型不是我们看到的.txt这类 二十指fifo,char,dir,block,link,file 和 unknown
$fp=fopen($file,"r");//打开文件
print_r(fstat($fp));//打印文件信息
echo "当前文件路径信息".__FILE__."
";
echo "当前文件所在的目录".dirname(__FILE__)."
";
echo "当前文件名".basename(__FILE__)."
";
print_r(stat($file));//打印文件信息
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)