php分享一个操作文件的类(包括创建文件、读文件和写文件)
时间:2016-08-11
本文章向大家分享一个php创建文件、读文件和写文件的php类,需要的朋友可以参考一下。
php创建文件、读文件和写文件,代码如下:
<?php
class cfile {
protected $thepath;
//Error messages in the form of constants for ease of use.
const FOUNDERROR = "Sorry, the file in question does not exist.";
const PERMERROR = "Sorry, you do not have the proper permissions on this file";
const OPENERROR = "Sorry, the file in question could not be opened.";
const CLOSEERROR = "Sorry, the file could not be closed.";
//The constructor function.
public function __construct (){
$num_args = func_num_args();
if($num_args > 0){
$args = func_get_args();
$this->thepath = $args[0];
}
}
//A function to open the file.
/* http://www.manongjc.com/article/1343.html */
private function openfile ($readorwrite){
//First, ensure the file exists.
try {
if (file_exists ($this->thepath)){
//Now, you need to see if you are reading or writing or both.
$proceed = false;
if ($readorwrite == "r"){
if (is_readable($this->thepath)){
$proceed = true;
}
} elseif ($readorwrite == "w"){
if (is_writable($this->thepath)){
$proceed = true;
}
} else {
if (is_readable($this->thepath) && is_writable($this->thepath)){
$proceed = true;
}
}
try {
if ($proceed){
//You can now attempt to open the file.
try {
if ($filepointer = fopen ($this->thepath, $readorwrite)){
return $filepointer;
} else {
throw new exception (self::OPENERROR);
return false;
}
} catch (exception $e) {
echo $e->getmessage();
}
} else {
throw new exception (self::PERMERROR);
}
} catch (exception $e) {
echo $e->getmessage();
}
} else {
throw new exception (self::FOUNDERROR);
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//A function to close a file.
function closefile () {
try {
if (!fclose ($this->thepath)){
throw new exception (self::CLOSEERROR);
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//A function to read a file, then return the results of the read in a string.
public function read () {
//First, attempt to open the file.
$filepointer = $this->openfile ("r");
//Now, return a string with the read data.
if ($filepointer != false){
//Then you can read the file.
return fread ($filepointer,filesize ($this->thepath));
}
//Lastly, close the file.
$this->closefile ();
}
//A function to write to a file.
public function write ($towrite) {
//First, attempt to open the file.
$filepointer = $this->openfile ("w");
//Now, return a string with the read data.
if ($filepointer != false){
//Then you can read the file.
return fwrite ($filepointer, $towrite);
}
//Lastly, close the file.
$this->closefile ();
}
//A function to append to a file.
public function append ($toappend) {
//First, attempt to open the file.
$filepointer = $this->openfile ("a");
//Now, return a string with the read data.
if ($filepointer != false){
//Then you can read the file.
return fwrite ($filepointer, $toappend);
}
//Lastly, close the file.
$this->closefile ();
}
//A function to set the path to a new file.
public function setpath ($newpath) {
$this->thepath = $newpath;
}
}
?>
<?php
//Include the class.
require_once ("file.class.inc.php");
//Then create a new instance of the class.
$myfile = new cfile ("data.txt");
//Now, let's try reading it.
echo $myfile->read();
//Then let's try writing to the file.
$myfile->write ("Hello World!");
//Then, let's try appending.
$myfile->append ("Hello Again!");
?>
- 41. select阻塞一个gorountine上的多个通讯操作 | 厚土Go学习笔记
- 40. range和channel的close | 厚土Go学习笔记
- 48. 访问MySql数据库增删改查和连接池及空字段处理 | 厚土Go学习笔记
- Go语言·我的性能我做主
- 量子计算对 bitcoin 的威胁
- 闲扯code review
- 你的Project Starter在哪里?
- Python网络_UDP编程
- Python网络_TCP/IP简介
- Python的itertools模块
- Python自建collections模块
- Zookeeper学习笔记——1 单机版本环境搭建
- 推荐系统指标评测——覆盖率与基尼系数的算法与应用
- 如何在Java应用中提交Spark任务?
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- yum安装本地rpm软件方案详解
- CentOS 部署 flask项目的方法
- 在linux服务器下使用版本控制软件SVN的方法
- centos中yum命令删除还原的补救方法介绍
- Linux 创建子进程执行任务的实现方法
- Linux系统下安装jdbc与tomcat的图文教程
- Linux系统下利用C程序输出某进程的内存占用信息
- .NET Standard中配置TargetFrameworks输出多版本类库
- .NET Standard SDK 样式项目中的目标框架
- 偿还技术债(2)-EventBus自己实现一个?
- AkShare-债券数据-收盘收益率曲线
- Vue 项目报错:‘$‘ is not defined ( no-undef )
- SpringBoot 项目开启热部署
- 小程序文件下载并保存文件名打开
- 分享个 CTF 小工具 bruteHASH