读写文件
//写入文件
$handler=fopen("data.txt","w+");
for ($i=1;$i<10;$i++){
fwrite($handler,"因为在{$i}年以后,世界早已忘了我\n",4096);
}
//file_put_contents("data.txt","无法深情挽着你的手",FILE_APPEND);//另一种写入方法,无需打开关闭文件句柄
fclose($handler);
//读取文件
$handler=fopen("data.txt","r") or die("打开失败");
while (!feof($handler)){ //feof()判断指针是否到达文件末尾
echo fgets($handler,4096); //一次读入一行 fgetc()一次读入一个字符
//echo fread($handle,filesize($file)); //另一种方法
}
//file_get_content(文件路径) //另一种方法
fclose($handler);
文件下载
public function downLoad()
{
$data = input('');
if (!empty($data)) {
$fileName = $this->createQRcode($data['shop_id'], $data['name'], $data['id'], $data['type'], $data['size']); //生成二维码,返回二维码图片链接
$contentType = 'image/png';
//print_r($filename);die;
$dir_path = '../public';
$fileurl = $dir_path . $fileName;
header("Cache-control: private");
header("Content-type: $contentType"); //设置要下载的文件类型
header("Content-Length:" . filesize($fileurl)); //设置要下载文件的文件大小
header("Content-Disposition: attachment; filename=" . urldecode("qrcode_for/shop/{$data['shop_id']}/type/{$data['type']}/gun/{$data['name']}/{$data['size']}.png")); //设置要下载文件的文件名
readfile($fileurl);
}
}
循环读取目录文件
function dirForeach($dir){
$handler = opendir($dir); //生成目录句柄
$fileArray=array();
while ($file = readdir($handler)) { //循环读取目录文件
if($file=="." || $file==".."){
continue;
}
array_push($fileArray,$file);
}
closedir($dir); //关闭目录句柄
return $fileArray;
}
循环计算目录大小(filesize)
function countDirSize($dir)
{
$size = 0;
if ($dirHandler = opendir($dir)) { //判断是否成功获得目录句柄
while ($file = readdir($dirHandler)) {
if ($file != "." && $file != "..") {
$filePath = $dir . DIRECTORY_SEPARATOR . $file; //找到目录下文件夹的完整路径,因为下面函数需要的参数是完整路径
//判断是否为文件或者目录
if (is_file($filePath)) { //如果是文件,累加大小
$size += filesize($filePath);
}
if (is_dir($filePath)) {
$size += countDirSize($filePath); //如果是目录,递归
}
}
}
closedir($dirHandler); //清空目录句柄
}
return $size;
}
$dir = "D:/CentOS6.6";
echo countDirSize($dir);
循环删除目录(unlink/rmdir)
function delDir($dir)
{
if (file_exists($dir)) {
if ($dirHandler = opendir($dir)) { //成功获得目录句柄
while ($file = readdir($dirHandler)) {
if ($file != "." && $file != "..") {
$filePath = $dir . DIRECTORY_SEPARATOR . $file; //找到目录下文件夹的完整路径,因为下面函数需要的参数是完整路径
if (is_file($filePath)) {
unlink($filePath);
}
if (is_dir($filePath)) {
delDir($filePath); //如果是目录,递归
}
}
}
closedir($dirHandler); //清空目录句柄
}
rmdir($dir);
}
}
$dir = "D:/test";
delDir($dir);
复制目录(copy)
function copyDir($srcDir, $destDir)
{
//判断目标是否是一个目录
if (is_file($destDir)) {
echo "目标是一个文件";
return;
}
//判断目标目录是否存在
if (!file_exists($destDir)) {
mkdir($destDir); //如果不存在就创建目录
}
if ($dirHandler = opendir($srcDir)) { //成功获得原目录句柄
while ($file = readdir($dirHandler)) {
if ($file != "." && $file != "..") {
//找到原目录和目标目录下文件夹的完整路径,因为下面函数需要的参数是完整路径
$srcFilePath = $srcDir . DIRECTORY_SEPARATOR . $file;
$destFilePath = $destDir . DIRECTORY_SEPARATOR . $file;
if (is_file($srcFilePath)) {
copy($srcFilePath, $destFilePath); //如果是文件就直接复制
}
if (is_dir($srcFilePath)) {
copyDir($srcFilePath, $destFilePath); //如果是目录,递归
}
}
}
closedir($dirHandler); //清空原目录的句柄
}
}
copyDir("hehe", "xixi");