LewaimaiFile.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. //这个里面存的是与文件或者目录操作相关的一些函数
  3. class LewaimaiFile {
  4. //创建目录(自动创建父目录)
  5. public static function mkdirs($dir)
  6. {
  7. if (!is_dir($dir))
  8. {
  9. if (!mkdirs(dirname($dir)))
  10. {
  11. return false;
  12. }
  13. if (!mkdir($dir, 0755))
  14. {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20. //获取文件的扩展名
  21. public static function get_extension($file)
  22. {
  23. return pathinfo($file, PATHINFO_EXTENSION);
  24. }
  25. //获取文件名
  26. public static function getFileName($filename, $file_content = '', $make=false)
  27. {
  28. return file_put_contents($filename, $file_content);
  29. if(!file_exists($filename) || $make){
  30. $int = file_put_contents($filename, $file_content);
  31. return $int !== false ? true : false;
  32. }
  33. return true;
  34. }
  35. /*
  36. * data 数据数组
  37. * $file_path 完整的cvs路径
  38. */
  39. public static function write_cvs($data, $file_path)
  40. {
  41. if(empty($data) || empty($file_path))
  42. {
  43. return false;
  44. }
  45. //取出目录
  46. $dir = pathinfo($file_path,PATHINFO_DIRNAME);
  47. //检查目录是否存在,否则递归创建目录
  48. if(!file_exists($dir))
  49. {
  50. mkdir($dir,0777,true);
  51. }
  52. $fp = fopen($file_path,'w');
  53. if (!$fp)
  54. {
  55. return false;
  56. }
  57. //先写入BOM头 (不需要)
  58. //fwrite($fp,"\xEF\xBB\xBF");
  59. foreach ($data as $row)
  60. {
  61. if (!is_array($row))
  62. {
  63. continue;
  64. }
  65. $tmp = array();
  66. foreach ($row as $val){
  67. $tmp[] = @iconv("utf-8//TRANSLIT//IGNORE", 'GBK', $val);
  68. }
  69. if (fputcsv($fp, $tmp) === false)
  70. {
  71. continue;
  72. }
  73. }
  74. fclose($fp);
  75. return true;
  76. }
  77. }