您的位置:首页 > 编程语言 > PHP开发

PHP解决方案@数据读写文本

2015-07-22 19:48 579 查看
方案解决目标:
把所需的数据写入到文本















数据写入文本






* 数据写入文本,一般有两种情况,要不就是字符串写入文本,要不就是数组(一维或者多维)写入文本


* 写入文本的方法有fwrite()和file_put_contents(),我选择后者


* 保证数据写入文本时是一个字符串,所以处理一维或者多维数组时,建议使用json格式封装数组数据,读取的时候解析json即可










// * 写入文本
$excel_json = json_encode ( $excel_data );// 写入文本时file_put_contents方法只能将一个字符串写入文件,如选择json格式

FileClass::checkDir ( AUTO_SYNC_PATH . 'rongxDemo/' ); // 检查目录是否存在                                                                       
$fileName = AUTO_SYNC_PATH . 'rongxDemo/' . "data.txt"; // 组装文件绝对路径,数据写入文本时文本的路径必须是绝对路径,不能是相对路径
                                                                                                                                               $file = file_put_contents ( $fileName, $excel_json ); // 写入数据








file_put_contents

(PHP 5, PHP 7)
file_put_contents — 将一个字符串写入文件



说明

int
file_put_contents (
string $filename ,
mixed $data [,
int $flags
= 0 [,
resource $context ]] )

和依次调用 fopen()fwrite() 以及 fclose() 功能一样。
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.



参数

filename
要被写入数据的文件名。 data
要写入的数据。类型可以是 stringarray 或者是 stream 资源(如上面所说的那样)。
如果 data 指定为 stream 资源,这里 stream 中所保存的缓存数据将被写入到指定文件中,这种用法就相似于使用stream_copy_to_stream() 函数。
参数 data 可以是数组(但不能为多维数组),这就相当于 file_put_contents($filename, join('', $array))。 flags
flags 的值可以是 以下 flag 使用 OR (|) 运算符进行的组合。 Available flags
Flag 描述
FILE_USE_INCLUDE_PATH 在 include 目录里搜索 filename。 更多信息可参见 include_path
FILE_APPEND 如果文件 filename 已经存在,追加数据而不是覆盖。
LOCK_EX 在写入时获得一个独占锁。
context
一个 context 资源。



返回值

该函数将返回写入到文件内数据的字节数,失败时返回FALSE

Warning
此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。




范例

Example #1 Simple usage example

<?php

$file = 'people.txt';

// Open the file to get existing content

$current = file_get_contents($file);

// Append a new person to the file

$current .= "John Smith\n";

// Write the contents back to the file

file_put_contents($file, $current);

?>

Example #2 Using flags

<?php

$file = 'people.txt';

// The new person to add to the file

$person = "John Smith\n";

// Write the contents to the file,

// using the FILE_APPEND flag to append the content to the end of the file

// and the LOCK_EX flag to prevent anyone else writing to the file at the same time

file_put_contents($file, $person, FILE_APPEND | LOCK_EX);

?>




















文本读取数据








* 读取文本的方法有fopen()和file_get_contents(),我选择后者


* 保证路径是绝对路径


* json数据解析






// * 读取文本
FileClass::checkDir ( AUTO_SYNC_PATH . 'rongxDemo/' ); // 检查目录是否存在

$fileName = AUTO_SYNC_PATH . 'rongxDemo/' . "data.txt"; // 组装文件绝对路径,数据读取文本时文本的路径也是要求是绝对路径

if (file_exists ( $fileName )) {
    $file_data = file_get_contents ( $fileName ); // 读取文本数据
}
$file_data = json_decode ( $file_data ); // 解析json封装过的数据







file_get_contents

(PHP 4 >= 4.3.0, PHP 5, PHP 7)
file_get_contents — 将整个文件读入一个字符串



说明

string

file_get_contents
(

string $filename
[,

bool $use_include_path = false
[,

resource $context
[,

int $offset = -1
[,

int $maxlen
]]]] )

file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

Note:
如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码。




参数

filename
要读取的文件的名称。 use_include_path

Note:
As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.
context
A valid context resource created with stream_context_create(). 如果你不需要自定义 context,可以用 NULL 来忽略。 offset
The offset where the reading starts on the original stream.
Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream. maxlen
Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.



返回值

The function returns the read data 或者在失败时返回 FALSE.



错误/异常

An E_WARNING level error is generated if either maxlength is less than zero, or if seeking to the specified offset in the stream fails.



范例

Example #1 Get and output the source of the homepage of a website

<?php

$homepage = file_get_contents('http://www.example.com/');

echo $homepage;

?>

Example #2 Searching within the include_path

<?php

// <= PHP 5

$file = file_get_contents('./people.txt', true);

// > PHP 5

$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);

?>

Example #3 Reading a section of a file

<?php

// Read 14 characters starting from the 21st character

$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);

var_dump($section);

?>

以上例程的输出类似于:


string(14) "lle Bjori Ro"



Example #4 Using stream contexts

<?php

// Create a stream

$opts = array(

'http'=>array(

'method'=>"GET",

'header'=>"Accept-language: en\r\n" .

"Cookie: foo=bar\r\n"

)

);



$context = stream_context_create($opts);



// Open the file using the HTTP headers set above

$file = file_get_contents('http://www.example.com/', false, $context);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: