PHP-读写文件操作

Yaurora

有时候经常用到文件的操作,又不想重复造轮子,这里主要记录下读写csv文件

文件读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function read_csv_file($file_path, $title_row = 0, $separator = ',', $enclosure = '"', $escape = '"'){
if(!file_exists($file_path)){
throw new Exception('File not exists');
}
$datas = [];
$handle = fopen($file_path, 'rb');
$row = 0;
$title = [];
while(($line = fgetcsv($handle, null, $separator, $enclosure, $escape)) !== false){
if ($title_row < 0){
$title = array_keys($line); // 数字键作为列
}
if ($row < $title_row){
continue;
}else if($row == $title_row){
$title = $line;
}else{
$temp = [];
foreach($title as $index => $field){
$temp[$field] = trim($line[$index]);
}
$datas[] = $temp;
}
$row++;
}
fclose($handle);
return $datas;
}

文件写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function write_csv_file($file_path, $datas = [], $separator = ',', $enclosure = '"', $escape = '"'){
if (empty($datas)){
throw new Exception("empty datas");
}
if (!file_exists($file_path)){
// create directory
$dirname = pathinfo($file_path, PATHINFO_DIRNAME);
if (!is_dir($dirname)){
mkdir(iconv('UTF-8','GBK', $dirname), 0777, true);
}
}

$title = array_keys($datas[0]);
$no_title = true;
$col = 0;
foreach($title as $t){
if ($t != $col){
$no_title = false;
break;
}
$col++;
}

$handle = fopen($file_path, 'w');
if (!$no_title){
// write table header
fputcsv($handle, $title, $separator, $enclosure, $escape);
}

foreach($datas as $data){
$content = [];
foreach($data as $txt){
if (!is_numeric($txt) && strtotime($txt) != false) {
$content[] = ' '.$txt;
}else{
$content[] = $txt;
}
}
fputcsv($handle, $content, $separator, $enclosure, $escape);
}
fclose($handle);
}

知识点

  • fopen

    1
    2
    3
    4
    5
    6
    fopen(
    string $filename,
    string $mode,
    bool $use_include_path = false,
    ?resource $context = null
    ): resource|false
  • fclose

    1
    fclose(resource $stream): bool
  • file_exists

    1
    file_exists(string $filename): bool
  • mkdir

    1
    2
    3
    4
    5
    6
    mkdir(
    string $directory,
    int $permissions = 0777,
    bool $recursive = false,
    ?resource $context = null
    ): bool
  • fputcsv

    1
    2
    3
    4
    5
    6
    7
    8
    fputcsv(
    resource $stream,
    array $fields,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\",
    string $eol = "\n"
    ): int|false
  • fgetcsv

    1
    2
    3
    4
    5
    6
    7
    fgetcsv(
    resource $stream,
    ?int $length = null,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\"
    ): array|false
  • pathinfo

    1
    pathinfo(string $path, int $flags = PATHINFO_ALL): array|string
  • 标题: PHP-读写文件操作
  • 作者: Yaurora
  • 创建于 : 2023-12-10 08:30:12
  • 更新于 : 2023-12-25 23:43:44
  • 链接: https://jingyu.life/2023/12/10/common-codes/php_file_operate/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
PHP-读写文件操作