php学习笔记:对文件的增删查改等操作

时间:2021-07-03 20:11:03

文件的创建:

采用touch()函数,当文件不存在会被创建

php学习笔记:对文件的增删查改等操作

例如:

<?php
header("Content-type: text/html; charset=utf-8");
//创建文件
$file_Mname='im.doc';
if(file_exists($file_Mname)){
echo "$file_Mname". "这个文件存在了!";
}else{
touch($file_Mname);
die("$file_Mname". "创建文件成功!");
}
?>

问题:中文名字的文件无法创建,这个我有时间再研究下这个问题的原因!

创建文件夹方式二:

<?php
header("Content-type: text/html; charset=utf-8"); if(!file_exists("files/创建的.xls")){ if(touch("files/a2.xls"))
echo "文件创建成功!";
else
echo "文件创建失败!"; }else{
echo "文件名已存在,请更名!";
} ?>

  读取文件大小的公共函数:

function transByte($size){
$arr =array(
"Bytes",
"KB",
"MB",
"GB",
"TB",
"EB"
);
$i=0;
while($size>1024){
$size/=1024;
$i++;
}
return round($size,2) . $arr[$i];
}