读写文件

  1. //写入文件
  2. $handler=fopen("data.txt","w+");
  3. for ($i=1;$i<10;$i++){
  4. fwrite($handler,"因为在{$i}年以后,世界早已忘了我\n",4096);
  5. }
  6. //file_put_contents("data.txt","无法深情挽着你的手",FILE_APPEND);//另一种写入方法,无需打开关闭文件句柄
  7. fclose($handler);
  8. //读取文件
  9. $handler=fopen("data.txt","r") or die("打开失败");
  10. while (!feof($handler)){ //feof()判断指针是否到达文件末尾
  11. echo fgets($handler,4096); //一次读入一行 fgetc()一次读入一个字符
  12. //echo fread($handle,filesize($file)); //另一种方法
  13. }
  14. //file_get_content(文件路径) //另一种方法
  15. fclose($handler);

文件下载

  1. public function downLoad()
  2. {
  3. $data = input('');
  4. if (!empty($data)) {
  5. $fileName = $this->createQRcode($data['shop_id'], $data['name'], $data['id'], $data['type'], $data['size']); //生成二维码,返回二维码图片链接
  6. $contentType = 'image/png';
  7. //print_r($filename);die;
  8. $dir_path = '../public';
  9. $fileurl = $dir_path . $fileName;
  10. header("Cache-control: private");
  11. header("Content-type: $contentType"); //设置要下载的文件类型
  12. header("Content-Length:" . filesize($fileurl)); //设置要下载文件的文件大小
  13. header("Content-Disposition: attachment; filename=" . urldecode("qrcode_for/shop/{$data['shop_id']}/type/{$data['type']}/gun/{$data['name']}/{$data['size']}.png")); //设置要下载文件的文件名
  14. readfile($fileurl);
  15. }
  16. }


循环读取目录文件

  1. function dirForeach($dir){
  2. $handler = opendir($dir); //生成目录句柄
  3. $fileArray=array();
  4. while ($file = readdir($handler)) { //循环读取目录文件
  5. if($file=="." || $file==".."){
  6. continue;
  7. }
  8. array_push($fileArray,$file);
  9. }
  10. closedir($dir); //关闭目录句柄
  11. return $fileArray;
  12. }

循环计算目录大小(filesize)

  1. function countDirSize($dir)
  2. {
  3. $size = 0;
  4. if ($dirHandler = opendir($dir)) { //判断是否成功获得目录句柄
  5. while ($file = readdir($dirHandler)) {
  6. if ($file != "." && $file != "..") {
  7. $filePath = $dir . DIRECTORY_SEPARATOR . $file; //找到目录下文件夹的完整路径,因为下面函数需要的参数是完整路径
  8. //判断是否为文件或者目录
  9. if (is_file($filePath)) { //如果是文件,累加大小
  10. $size += filesize($filePath);
  11. }
  12. if (is_dir($filePath)) {
  13. $size += countDirSize($filePath); //如果是目录,递归
  14. }
  15. }
  16. }
  17. closedir($dirHandler); //清空目录句柄
  18. }
  19. return $size;
  20. }
  21. $dir = "D:/CentOS6.6";
  22. echo countDirSize($dir);

循环删除目录(unlink/rmdir)

  1. function delDir($dir)
  2. {
  3. if (file_exists($dir)) {
  4. if ($dirHandler = opendir($dir)) { //成功获得目录句柄
  5. while ($file = readdir($dirHandler)) {
  6. if ($file != "." && $file != "..") {
  7. $filePath = $dir . DIRECTORY_SEPARATOR . $file; //找到目录下文件夹的完整路径,因为下面函数需要的参数是完整路径
  8. if (is_file($filePath)) {
  9. unlink($filePath);
  10. }
  11. if (is_dir($filePath)) {
  12. delDir($filePath); //如果是目录,递归
  13. }
  14. }
  15. }
  16. closedir($dirHandler); //清空目录句柄
  17. }
  18. rmdir($dir);
  19. }
  20. }
  21. $dir = "D:/test";
  22. delDir($dir);

复制目录(copy)

  1. function copyDir($srcDir, $destDir)
  2. {
  3. //判断目标是否是一个目录
  4. if (is_file($destDir)) {
  5. echo "目标是一个文件";
  6. return;
  7. }
  8. //判断目标目录是否存在
  9. if (!file_exists($destDir)) {
  10. mkdir($destDir); //如果不存在就创建目录
  11. }
  12. if ($dirHandler = opendir($srcDir)) { //成功获得原目录句柄
  13. while ($file = readdir($dirHandler)) {
  14. if ($file != "." && $file != "..") {
  15. //找到原目录和目标目录下文件夹的完整路径,因为下面函数需要的参数是完整路径
  16. $srcFilePath = $srcDir . DIRECTORY_SEPARATOR . $file;
  17. $destFilePath = $destDir . DIRECTORY_SEPARATOR . $file;
  18. if (is_file($srcFilePath)) {
  19. copy($srcFilePath, $destFilePath); //如果是文件就直接复制
  20. }
  21. if (is_dir($srcFilePath)) {
  22. copyDir($srcFilePath, $destFilePath); //如果是目录,递归
  23. }
  24. }
  25. }
  26. closedir($dirHandler); //清空原目录的句柄
  27. }
  28. }
  29. copyDir("hehe", "xixi");