PHP打开文件于读取修改小方法
2016-12-29 18:25
239 查看
function set_data($array,$url) {
if (is_array($array)) {
I($array);
$txt_file = $url; //txt文件
$arr = json_decode(file_get_contents($txt_file), TRUE); //将txt文件文本输出并转换成数组
foreach ($array as $key => $value) {
$arr[$key] = urlencode($value);
}
$json_text = json_encode($arr); //将数组转成json
$myfile = fopen($txt_file, "w"); //打开txt文件,以覆盖的方式写入
$res = fwrite($myfile, $json_text); //写入数据
fclose($myfile); //关闭文件
if ($res) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
function get_data($akey = '') {
if ($akey == '') { //获取全部
$txt_file = "./TxtDabase/data.json";
$all_res = json_decode(file_get_contents($txt_file), TRUE);
foreach ($all_res as $key => $value) {
$ret_arr[$key]= urldecode($value);
}
return $ret_arr;
} else { //获取指定
$txt_file = "./TxtDabase/data.json";
$res = json_decode(file_get_contents($txt_file), TRUE);
if ($res[$akey]) {
return urldecode($res[$akey]);
} else {
return FALSE;
}
}
}
if (is_array($array)) {
I($array);
$txt_file = $url; //txt文件
$arr = json_decode(file_get_contents($txt_file), TRUE); //将txt文件文本输出并转换成数组
foreach ($array as $key => $value) {
$arr[$key] = urlencode($value);
}
$json_text = json_encode($arr); //将数组转成json
$myfile = fopen($txt_file, "w"); //打开txt文件,以覆盖的方式写入
$res = fwrite($myfile, $json_text); //写入数据
fclose($myfile); //关闭文件
if ($res) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
function get_data($akey = '') {
if ($akey == '') { //获取全部
$txt_file = "./TxtDabase/data.json";
$all_res = json_decode(file_get_contents($txt_file), TRUE);
foreach ($all_res as $key => $value) {
$ret_arr[$key]= urldecode($value);
}
return $ret_arr;
} else { //获取指定
$txt_file = "./TxtDabase/data.json";
$res = json_decode(file_get_contents($txt_file), TRUE);
if ($res[$akey]) {
return urldecode($res[$akey]);
} else {
return FALSE;
}
}
}
相关文章推荐
- php读取文件:PHP读取COOKIES的实现方法
- word无法读取此文档,文档可能已损坏 请尝试下列方法:*打开并修复文件*用文本恢复转换器打开文件
- asp.net中打开Excel上传文件,读取数据的方法
- java中打开指定文件夹及读取文件方法
- 用PHP读取文件的正确方法
- 用 PHP 读取文件的正确方法
- 用PHP读取文件的正确方法
- 修改PHP上传文件大小限制的方法
- 用PHP读取文件的正确方法
- php文件的打开,读取,关闭
- 用PHP读取文件的正确方法
- 用 PHP 读取文件的正确方法
- 一起学习用PHP读取文件的正确方法
- PHP——Eclipse如何修改打开PHP文件时有编码方式
- 读取文件并修改文件创建并插入到另一文件的方法
- 用 PHP 读取文件的正确方法
- 用PHP读取文件的正确方法(4)
- PHP 读取文件的正确方法
- 修改php.ini实现Mysql导入数据库文件最大限制的修改方法
- 移动硬盘无法访问,打开提示”文件或目录损坏且无法读取”的处理方法