| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- //这个里面存的是与文件或者目录操作相关的一些函数
- class LewaimaiFile {
- //创建目录(自动创建父目录)
- public static function mkdirs($dir)
- {
- if (!is_dir($dir))
- {
- if (!mkdirs(dirname($dir)))
- {
- return false;
- }
- if (!mkdir($dir, 0755))
- {
- return false;
- }
- }
- return true;
- }
-
- //获取文件的扩展名
- public static function get_extension($file)
- {
- return pathinfo($file, PATHINFO_EXTENSION);
- }
-
- //获取文件名
- public static function getFileName($filename, $file_content = '', $make=false)
- {
- return file_put_contents($filename, $file_content);
- if(!file_exists($filename) || $make){
- $int = file_put_contents($filename, $file_content);
- return $int !== false ? true : false;
- }
-
- return true;
- }
- /*
- * data 数据数组
- * $file_path 完整的cvs路径
- */
- public static function write_cvs($data, $file_path)
- {
- if(empty($data) || empty($file_path))
- {
- return false;
- }
-
- //取出目录
- $dir = pathinfo($file_path,PATHINFO_DIRNAME);
-
- //检查目录是否存在,否则递归创建目录
- if(!file_exists($dir))
- {
- mkdir($dir,0777,true);
- }
- $fp = fopen($file_path,'w');
- if (!$fp)
- {
- return false;
- }
-
- //先写入BOM头 (不需要)
- //fwrite($fp,"\xEF\xBB\xBF");
-
- foreach ($data as $row)
- {
- if (!is_array($row))
- {
- continue;
- }
-
- $tmp = array();
- foreach ($row as $val){
- $tmp[] = @iconv("utf-8//TRANSLIT//IGNORE", 'GBK', $val);
- }
-
- if (fputcsv($fp, $tmp) === false)
- {
- continue;
- }
- }
-
- fclose($fp);
-
- return true;
- }
- }
|