PHP数据库操作之简单学习
2016-03-24 13:37
656 查看
1 打开关闭文件
fopen()
resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
fopen()函数将resource绑定到一个流或句柄。绑定之后,脚本就可以通过句柄与此资源交互;
例1:以只读方式打开一个位于本地服务器的文本文件
$fh = fopen("test.txt", "r");
例2:以只读方式打开一个远程文件
$fh = fopen("http://www.baidu.com", "r");
fclose()
bool fclose ( resource handle )
将 handle 指向的文件关闭 。如果成功则返回 TRUE,失败则返回 FALSE;
文件指针必须有效,并且是通过 fopen() 或 fsockopen() 成功打开的;
虽然每个请求最后都会自动关闭文件,但明确的关闭打开的所有文件是一个好的习惯;
例:
$fh = fopen("test.txt", "r");
fclose($fh);12
2 读取文件
php 提供了很多从文件中读取数据的方法,不仅可以一次只读取一个字符,还可以一次读取整个文件。
fread()
string fread ( int handle, int length )
fread()函数从handle指定的资源中读取length个字符,
当到达EOF或读取到length个字符时读取将停止。
如果要读取整个文件,使用filesize()函数确定应该读取的字符数;
file()
array file ( string $filename [, int $flags = 0 [, resource $context ]])
file()函数将文件读取到数组中,各元素由换行符分隔。
例:
$arr = file("test.txt");
print_r($arr);12
file_get_contents()
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )
file_get_contents()函数将文件内容读到字符串中;
例:
$str = file_get_contents("test.txt");
echo $str;12
3 写入文件
fwrite()
int fwrite ( resource handle, string string [, int length] )
fwrite()函数将string的内容写入到由handle指定的资源中。
如果指定length参数,将在写入Length个字符时停止。
例:
$str = "test text";
$fh = fopen("test.txt", "a");
fwrite($fh, $str);
fclose($fh);1234
file_put_contents()
int file_put_contents ( string filename, string data [, int flags [, resource context]] )
file_put_contents()函数将一个字符串写入文件,与依次调用fopen(),fwrite(),fclose()功能一样;
例:
$str = "hello";
file_put_contents("test.txt", $str);12
4 复制,重命名,删除文件
copy()
bool copy ( string source, string dest )
将文件从 source 拷贝到 dest。如果成功则返回 TRUE,失败则返回 FALSE。
例:Copy("test.txt", "test.txt.bak");
rename()
bool rename ( string oldname, string newname [, resource context] )
尝试把 oldname 重命名为 newname。 如果成 功则返回 TRUE,失败则返回 FALSE。
例:rename("test.txt", “test2.txt”);
unlink()
bool unlink ( string filename )
删除文件,如果删除成功返回true, 否则返回false;
5读取文件
fopen()
resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
fopen()函数将resource绑定到一个流或句柄。绑定之后,脚本就可以通过句柄与此资源交互;
例1:以只读方式打开一个位于本地服务器的文本文件
$fh = fopen("test.txt", "r");
例2:以只读方式打开一个远程文件
$fh = fopen("http://www.baidu.com", "r");
fclose()
bool fclose ( resource handle )
将 handle 指向的文件关闭 。如果成功则返回 TRUE,失败则返回 FALSE;
文件指针必须有效,并且是通过 fopen() 或 fsockopen() 成功打开的;
虽然每个请求最后都会自动关闭文件,但明确的关闭打开的所有文件是一个好的习惯;
例:
$fh = fopen("test.txt", "r");
fclose($fh);12
2 读取文件
php 提供了很多从文件中读取数据的方法,不仅可以一次只读取一个字符,还可以一次读取整个文件。
fread()
string fread ( int handle, int length )
fread()函数从handle指定的资源中读取length个字符,
当到达EOF或读取到length个字符时读取将停止。
如果要读取整个文件,使用filesize()函数确定应该读取的字符数;
file()
array file ( string $filename [, int $flags = 0 [, resource $context ]])
file()函数将文件读取到数组中,各元素由换行符分隔。
例:
$arr = file("test.txt");
print_r($arr);12
file_get_contents()
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )
file_get_contents()函数将文件内容读到字符串中;
例:
$str = file_get_contents("test.txt");
echo $str;12
3 写入文件
fwrite()
int fwrite ( resource handle, string string [, int length] )
fwrite()函数将string的内容写入到由handle指定的资源中。
如果指定length参数,将在写入Length个字符时停止。
例:
$str = "test text";
$fh = fopen("test.txt", "a");
fwrite($fh, $str);
fclose($fh);1234
file_put_contents()
int file_put_contents ( string filename, string data [, int flags [, resource context]] )
file_put_contents()函数将一个字符串写入文件,与依次调用fopen(),fwrite(),fclose()功能一样;
例:
$str = "hello";
file_put_contents("test.txt", $str);12
4 复制,重命名,删除文件
copy()
bool copy ( string source, string dest )
将文件从 source 拷贝到 dest。如果成功则返回 TRUE,失败则返回 FALSE。
例:Copy("test.txt", "test.txt.bak");
rename()
bool rename ( string oldname, string newname [, resource context] )
尝试把 oldname 重命名为 newname。 如果成 功则返回 TRUE,失败则返回 FALSE。
例:rename("test.txt", “test2.txt”);
unlink()
bool unlink ( string filename )
删除文件,如果删除成功返回true, 否则返回false;
5读取文件
相关文章推荐
- php 自带过滤和转义函数
- PHP5中的stdClass
- php 抽奖程序
- Alluxio源码分析定位策略:指定主机策略SpecificHostPolicy
- PHP的错误机制总结
- Sphider + SCWS 打造完美PHP中文搜索引擎
- php new self()和new static()
- PHP - Traversable接口详解
- Zend Studio / Ecliplse插件EasyExplorer,打开资源文件所在的文件夹
- PHP错误机制知识汇总
- Windows 7如何建立一个FTP的快捷方式
- PHP底层的运行机制与原理
- php栈和队列
- YII2 模型概述
- mod_PHP&fastcgi
- Php导出excle文件
- php 文件上传失败
- php之逻辑判断
- php中curl、fsockopen的应用
- php 判断是否是ajax提交的方法