设为首页收藏本站--- 驰名中外的国际土木工程技术交流平台!

东南西北人

 找回密码
 注册

QQ登录

只需一步,快速开始

总共8807条微博

动态微博

本站帖子精华之精华汇总 Best of the Best英语口语、听力、翻译、考试学习经验交流与探讨1000多土木工程类行业软件、计算表格和计算工具免费下载东南西北人网站QQ精英群 QQ189615688
中国土木工程师手册(上中下)东南西北人英文资料走马观花500多专业手册、工程手册100多个专业词典大汇总
如何获取积分和金币?精彩施工和土木工程技术视频东南西北人英汉对照资料汇总各版块精彩讨论贴汇总!
查看: 1087|回复: 1

基本的文件在PHP处理

[复制链接]
鲜花(279) 鸡蛋(4)
ljmtidilgw 发表于 2010-10-30 13:58:19 | 显示全部楼层 |阅读模式
在本教程中,我们想学基本的PHP机制及文件处理。
File handling functions in PHP are extremely usefull and userfriendly.
在PHP文件处理功能是非常有用和用户友好。
Many websites use simple text files as there data store because it has following advantages
许多网站使用,因为数据存储简单的文本文件,因为它具有以下优点
1) Minimization of cost.
1)成本最小化。
Cost of maintaining databases like Oracle are huge, thus files are low cost answer to these databases.
像Oracle数据库的维护成本是巨大的,因此文件低成本回答这些数据库。
2) Taking back Up of your database is very easy.
2)占用了你的数据库备份是很容易的。
All you have to do is to copy the file.
所有您需要做的就是复制文件。
3) It much easier to transfer a text file from one OS to another.
3)更容易从一个OS转移一个文本文件到另一个。
However there are divadvantages too
不过也有divadvantages
1) Text files do not support SQL Query languages.
1)文本文件不支持SQL查询语言。
Thus you will have to code your data retrieval and updation algorithms yourself.
这样,你将不得不编写您的数据检索和updation算法自己。
2) It does not use any indexing mechanism , thus searching mechanism is much slower.
2)不使用任何索引机制,从而搜索机制是慢得多。
3) You cannot use triggers, define primary keys , Foreign Keys etc.
3)您不能使用触发器,定义主键,外键等
Now, lets start to see how PHP handles files.
现在,让我们开始看到PHP如何处理文件。
1) Opening a file.
1)打开文件。
In PHP you can open a file using the function call
在PHP中,你可以打开一个文件使用函数调用
fopen ( "filename","mode")
的FOPEN(“文件名”,“模式”)
Here "mode" means whether you want to open the file in read mode, write mode or append mode.
这里的“模式”是指无论你想读模式打开文件,写模式或追加模式。
The values of this parameters are defined as
这个参数的值被定义为
A list of possible modes for fopen() using mode
一种可能的模式适用于名单的FOPEN()使用模式
mode Description
模式的说明
'r'
为'r'
Open for reading only; place the file pointer at the beginning of the file.
以只读方式打开,放置在文件开头的文件指针。
'r+'
为'r +'
Open for reading and writing; place the file pointer at the beginning of the file.
打开阅读和写作,在文件的开始位置文件指针。
'w'
'w'的
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length.
打开只写;放置在该文件的开头文件指针并截断该文件的长度为零。
If the file does not exist, attempt to create it.
如果该文件不存在,试图去创造。
'w+'
'瓦特+'
Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length.
打开阅读和写作,放置在文件的开头文件指针并截断该文件的长度为零。
If the file does not exist, attempt to create it.
如果该文件不存在,试图去创造。
'a'
'一'
Open for writing only; place the file pointer at the end of the file.
打开只写;放置在文件的末尾文件指针。
If the file does not exist, attempt to create it.
如果该文件不存在,试图去创造。
'a+'
'一+'
Open for reading and writing; place the file pointer at the end of the file.
打开阅读和写作,在文件结尾处的文件指针。
If the file does not exist, attempt to create it.
如果该文件不存在,试图去创造。
'x'
'X'号
Create and open for writing only; place the file pointer at the beginning of the file.
创建并打开只写,放置在文件的开头文件指针。
If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING .
如果文件已存在, 则fopen()调用失败并返回FALSE并产生一条E_WARNING误差水平。
If the file does not exist, attempt to create it.
如果该文件不存在,试图去创造。
This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
这相当于指定O_EXCL |底层的open(2)系统调用O_CREAT标记。
This option is supported in PHP 4.3.2 and later, and only works for local files.
此选项在PHP 4.3.2和支持后,只为本地文件工作。
'x+'
'× +'
Create and open for reading and writing; place the file pointer at the beginning of the file.
创建并打开阅读和写作,放置在文件的开头文件指针。
If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING .
如果文件已存在, 则fopen()调用失败并返回FALSE并产生一条E_WARNING误差水平。
If the file does not exist, attempt to create it.
如果该文件不存在,试图去创造。
This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
这相当于指定O_EXCL |底层的open(2)系统调用O_CREAT标记。
This option is supported in PHP 4.3.2 and later, and only works for local files.
此选项在PHP 4.3.2和支持后,只为本地文件工作。
Example code for reading from a files
示例代码可以从一个文件读取数据
<?php $file_name = "data.dat";
<PHP的$ file_name的=“data.dat?”;
// absolute path : \home\data.dat
/ /绝对路径:\首页\ data.dat
$file_pointer = fopen($file_name, "r");
$ file_pointer =的FOPEN($ file_name中,“住宅”);
//Open the file in readonly mode,
/ /打开文件在只读模式,
//check if you read permission in unix
/ /检查是否在你读UNIX权限
$file_read = fread($file_pointer, filesize($file_name));
从文件中读file_read = $($ file_pointer,文件大小($ file_name中));
echo $file_read;
回声$ file_read;
// Reading the file contents, through the pointer
/ /读文件的内容,通过指针
fclose($file_pointer);
fclose($ file_pointer);
// close the file
/ /关闭文件
?>
?>
Note: If the file resides in the some other folder than the php file then you have to give the full pathname.
注意:如果文件中的一些其他文件夹以外的php文件,那么你必须给所在的完整路径。
Now suppose fopen was unable to open the file then you may give user a message.
现在假设的FOPEN无法打开文件,那么你可以给用户一个消息。
Thus the code becomes
因此,代码变成
<?php $file_name = "data.dat";
<PHP的$ file_name的=“data.dat?”;
// absolute path : \home\data.dat
/ /绝对路径:\首页\ data.dat
$file_pointer = fopen($file_name, "r");
$ file_pointer =的FOPEN($ file_name中,“住宅”);
if (!$file_pointer) die ("could not open file $file_name");
(!$ file_pointer)如果死(“无法打开文件$ file_name中”);
//Open the file in readonly mode,
/ /打开文件在只读模式,
//check if you read permission in unix
/ /检查是否在你读UNIX权限
$file_read = fread($file_pointer, filesize($file_name));
从文件中读file_read = $($ file_pointer,文件大小($ file_name中));
// Reading the file contents, through the pointer
/ /读文件的内容,通过指针
echo $file_read;
回声$ file_read;
// print the contents of the file;
/ /打印文件的内容;
fclose($file_pointer);
fclose($ file_pointer);
// close the file
/ /关闭文件
?>
?>
Writing to a file:
写入文件:
<?php
<?PHP的
$file_name = "data.txt";
$ file_name的=“data.txt”;
$file_pointer = fopen($file_name, "w+");
$ file_pointer =的FOPEN($ file_name中,“宽+”);
// "w+" is the write mode, or the action we're going to perform on the file
/ /“w +”将是写模式或行动,我们将要执行的文件
// "w+" mode clears the existing file content to 0 bytes
/ /的“W +”模式,清除现有的文件的内容为0字节
fwrite($file_pointer, "PHP is power ");
使用fwrite($ file_pointer,“PHP是电力”);
// write something to the file
/ /写入到文件的东西
fclose($file_pointer);
fclose($ file_pointer);
print "data written to file successfully";
打印“写入文件成功数据”;
?>
?>
As you see whenever we open the file in write mode the previous contents are getting truncated.
正如你所看到,每当我们写模式中打开以前的内容越来越截断文件。
If you want to keep the previous file contents, then you have to open the file in "append" mode.
如果您想保留以前的文件内容,那么你必须打开“追加”模式的文件。
Opening the file in append mode:
在追加模式打开文件:
<?php
<?PHP的
$file_name = "data.txt";
$ file_name的=“data.txt”;
$file_pointer = fopen($file_name, "a");
$ file_pointer =的FOPEN($ file_name中,“一”);
// "a" is the append mode
/ /“A”是追加模式
// "a" mode keeps the existing file content and starts writing from the end of the file
/ /“A”的模式将保持现有的文件的内容,并开始从该文件写入结束
fwrite($file_pointer, "PHP is power ");
使用fwrite($ file_pointer,“PHP是电力”);
// write something to the file
/ /写入到文件的东西
fclose($file_pointer);
fclose($ file_pointer);
print "data written to file successfully";
打印“写入文件成功数据”;
?>
?>
With these basic basic knowledge of file handling we can try to perform some database maintenance operation through files.
有了这些基本的文件处理的基本知识,我们可以尝试通过文件执行某些数据库维护操作。
Suppose the name of your database file is "data.txt" and records are stored in the format
假设您的数据库文件的名称为“data.txt”和记录保存的格式

001 | Ravi | India \n
001 |拉维|印度\ &ntilde;
002 | Steve| Australia \n
002 |史蒂夫|澳大利亚\ &ntilde;
003 | Paul | France \n
003 |保罗|法国\ &ntilde;
004 |Dustin|USA\n
004 |达斯汀|美国\ &ntilde;
005 |Jatin |India \n
005 | Jatin |印度\ &ntilde;
Printing all the records
打印所有记录
Now , if you want to print the employee information then the code will be
现在,如果你想打印员工信息,然后代码将被
<?php
<?PHP的
$filename = "data.txt";
$文件名=“data.txt”;
$fp = fopen($filename, "r") or die("Couldn't open $filename");
= $计划生育的FOPEN($文件,“住宅”)或死亡(“无法打开$文件名”);
while(!feof($fp))
而(!feof($计划生育))
{ $line = fgets($fp);
{$行= fgets($ FP)的;
print "$line<br>";
打印“$线<br>物理化学”;
}
}
fclose($fp);
fclose($ FP)的;
?>
?>
Searching a particuler record in a database file :
搜索的数据库文件particuler记录在:
Suppose you want to search the record of Ravi in the database.
假设你要搜索的数据库中的拉维纪录。
<?php
<?PHP的
$filename = "data.txt";
$文件名=“data.txt”;
$fp = fopen($filename, "r") or die("Couldn't open $filename");
= $计划生育的FOPEN($文件,“住宅”)或死亡(“无法打开$文件名”);
while(!feof($fp))
而(!feof($计划生育))
{ $line = fgets($fp);
{$行= fgets($ FP)的;
if (preg_match('/Ravi/',$line)) // Print the line if it contains the word 'Ravi'
如果(preg_match('/拉维/',$行))/ /打印行,如果它包含单词'拉维'
print "$line<br>";
打印“$线<br>物理化学”;
}
}
fclose($fp);
fclose($ FP)的;
?>
?>
Editing a record in database file
编辑一条记录在数据库文件
Suppose you want to Edit the record of 'Steve' with the value
假设你要编辑的值的'史蒂夫的纪录
002 | Steve| Russia \n
002 |史蒂夫|俄罗斯\ &ntilde;
<?php
<?PHP的
$new_data = "002 | Steve| Russia \r\n";// "\r\n " is the line break
$ new_data =“002 |史蒂夫|俄罗斯\ ř \ N”的; / /“\ ř \ n”是换行符
$filename_original = "data.txt";
$ filename_original =“data.txt”;
$filename_edited = "datacopy.txt" ;
$ filename_edited =“datacopy.txt”;
$fporiginal = fopen($filename_original, "r") or die("Couldn't open $filename_original");
$ fporiginal =的FOPEN($ filename_original,“住宅”)或死亡(“无法打开$ filename_original”);
$fpedited = fopen($filename_edited, "w") or die("Couldn't open $filename_edited");
$ fpedited =的FOPEN($ filename_edited,“宽”)或死亡(“无法打开$ filename_edited”);
while(!feof($fporiginal))
而(!feof($ fporiginal))
{ $line = fgets($fporiginal);
{$行= fgets($ fporiginal);
if (preg_match('/Steve/',$line)) // search the line if it contains the word 'Steve'
如果(preg_match('/史蒂夫/',$行))/ /搜索的行,如果它包含单词'史蒂夫'
fwrite($fpedited,$new_data);
使用fwrite($ fpedited,$ new_data);
else
其他
fwrite($fpedited,$line);
使用fwrite($ fpedited,$线);
//if the line does not contain the word 'Steve' the write back the original line
/ /如果该行不包含单词'史蒂夫'的写回原线
}
}
fclose($fporiginal);
fclose($ fporiginal);
fclose($fpedited);
fclose($ fpedited);
// now copy the contents of 'datacopy.txt' to 'data.txt'
/ /现在复制datacopy.txt'的内容'到'data.txt'
$filename_original = "data.txt";
$ filename_original =“data.txt”;
$filename_edited = "datacopy.txt" ;
$ filename_edited =“datacopy.txt”;
$fporiginal = fopen($filename_original, "w+") or die("Couldn't open $filename_original");
$ fporiginal =的FOPEN($ filename_original,“宽+”)或死亡(“无法打开$ filename_original”);
$fpedited = fopen($filename_edited, "r") or die("Couldn't open $filename_edited");
$ fpedited =的FOPEN($ filename_edited,“住宅”)或死亡(“无法打开$ filename_edited”);
$data = fread($fpedited, filesize($filename_edited));
从文件中读数据= $($ fpedited,文件大小($ filename_edited));
fwrite($fporiginal ,$data);
使用fwrite($ fporiginal,$数据);
fclose($fporiginal);
fclose($ fporiginal);
fclose($fpedited);
fclose($ fpedited);
unlink($filename_edited); //delete the file datacopy.txt
断开($ filename_edited)/ /删除该文件datacopy.txt
?>
?>
Deleteting a record from database file
Deleteting一个文件记录从数据库
Suppose you want to delete the record of 'Steve'from the file , then the code will be
假设你要删除的'Steve'from记录的文件,那么代码将被
<?php
<?PHP的
$filename_original = "data.txt";
$ filename_original =“data.txt”;
$filename_edited = "datacopy.txt" ;
$ filename_edited =“datacopy.txt”;
$fporiginal = fopen($filename_original, "r") or die("Couldn't open $filename_original");
$ fporiginal =的FOPEN($ filename_original,“住宅”)或死亡(“无法打开$ filename_original”);
$fpedited = fopen($filename_edited, "w") or die("Couldn't open $filename_edited");
$ fpedited =的FOPEN($ filename_edited,“宽”)或死亡(“无法打开$ filename_edited”);
while(!feof($fporiginal))
而(!feof($ fporiginal))
{ $line = fgets($fporiginal);
{$行= fgets($ fporiginal);
if (!preg_match('/Steve/',$line)) // search the line if it contains the word 'Steve'
如果(!preg_match('/史蒂夫/',$行))/ /搜索的行,如果它包含单词'史蒂夫'
fwrite($fpedited,$line);
使用fwrite($ fpedited,$线);
//if the line does not contain the word 'Steve' the write back the original line
/ /如果该行不包含单词'史蒂夫'的写回原线
}
}
fclose($fporiginal);
fclose($ fporiginal);
fclose($fpedited);
fclose($ fpedited);
// now copy the contents of 'datacopy.txt' to 'data.txt'
/ /现在副本'datacopy.txt'的内容'data.txt'
$filename_original = "data.txt";
$ filename_original =“data.txt”;
$filename_edited = "datacopy.txt" ;
$ filename_edited =“datacopy.txt”;
$fporiginal = fopen($filename_original, "w+") or die("Couldn't open $filename_original");
$ fporiginal =的FOPEN($ filename_original,“宽+”)或死亡(“无法打开$ filename_original”);
$fpedited = fopen($filename_edited, "r") or die("Couldn't open $filename_edited");
$ fpedited =的FOPEN($ filename_edited,“住宅”)或死亡(“无法打开$ filename_edited”);
$data = fread($fpedited, filesize($filename_edited));
从文件中读数据= $($ fpedited,文件大小($ filename_edited));
fwrite($fporiginal ,$data);
使用fwrite($ fporiginal,$数据);
fclose($fporiginal);
fclose($ fporiginal);
fclose($fpedited);
fclose($ fpedited);
unlink($filename_edited);
断开($ filename_edited);
//delete the file datacopy.txt
/ /删除该文件datacopy.txt
?>
?>
In the above case when PHP writes to info.file for Rob, Bill also starts writing and at that instance when '\n' for Rob was still to be written.
在上述情况下当PHP写info.file为Rob,比尔也开始写作,在那个例子,当为'\ n'为Rob仍然要写入。
Causing a file sequence break!
造成文件序列突破!
We don't want this happening, do we ?
我们不希望这种情况发生,是吗?
I'll take NO as the answer for the sake of progressing.
我将不采取任何作为而回答的进展。
So, let's look at file locking :
所以,让我们来看看文件锁定:
<?php
<?PHP的
$file_name = "data.dat";
$ file_name的=“data.dat”;
$file_pointer = fopen($file_name, "r");
$ file_pointer =的FOPEN($ file_name中,“住宅”);
$lock = flock($file_pointer, LOCK_SH);
羊群锁= $($ file_pointer,LOCK_SH);
// I use 4.0.2 so LOCK_SH or you can use 1.
/ /我用4.0.2所以LOCK_SH或您可以使用1。
if ($lock) {
如果($锁){
$file_read = fread($file_pointer, filesize($file_name));
从文件中读file_read = $($ file_pointer,文件大小($ file_name中));
$lock = flock($file_pointer, LOCK_UN);
羊群锁= $($ file_pointer,LOCK_UN);
// Use 3 if < PHP4.0.2
/ /使用3如果<PHP4.0.2
}
}
fclose($file_pointer);
fclose($ file_pointer);
print "file contents are $file_read";
打印“文件的内容是$ file_read”;
?gt;
?总吨位;
In the above if 2 files read.php and read2.php access the file then both of them will be able to read it, but incase a program needs to write to the file it will have to wait till the read operation finishes and the lock is released.
如果在上述两个文件read.php和read2.php访问文件,那么他们两人将能够阅读,但柜面程序需要写入文件,它将不得不等到读操作完成和锁定被释放。
<?php
<?PHP的
$file_name = "data.dat";
$ file_name的=“data.dat”;
$file_pointer = fopen($file_name, "w");
$ file_pointer =的FOPEN($ file_name中,“宽”);
$lock = flock($file_pointer, LOCK_EX);
羊群锁= $($ file_pointer,独占的);
// Use 2 if you use < PHP4.0.2
/ /使用2如果您使用<PHP4.0.2
if ($lock) {
如果($锁){
fwrite($file_pointer, "what u wanna write");
使用fwrite($ file_pointer,“想写什么ü”);
flock($file_pointer, LOCK_UN);
羊群($ file_pointer,LOCK_UN);
// Use 3 < PHP4.0.2
/ /使用3 <PHP4.0.2
}
}
fclose($file_pointer);
fclose($ file_pointer);
print "data written to file successfully";
打印“写入文件成功数据”;
?gt;
?总吨位;
Though using "w" is only used for overwriting data and to me can't be put to practical use!
尽管使用的“W”只用于覆盖数据,以及我无法投入实际应用!
<?php
<?PHP的
$file_name = "data.dat";
$ file_name的=“data.dat”;
$file_pointer = fopen($file_name, "a");
$ file_pointer =的FOPEN($ file_name中,“一”);
$lock = flock($file_pointer, LOCK_EX);
羊群锁= $($ file_pointer,独占的);
// Use 2 if you use < PHP4.0.2
/ /使用2如果您使用<PHP4.0.2
if ($lock) {
如果($锁){
fseek($file_pointer, 0, SEEK_END);
fseek($ file_pointer,0,SEEK_END);
// Use this if < PHP4.0RC1 : fseek($file_pointer, filsize($file_name));
/ /使用此如果<PHP4.0RC1:fseek($ file_pointer,filsize($ file_name中));
fwrite($file_pointer, "what u wanna write");
使用fwrite($ file_pointer,“想写什么ü”);
flock($file_pointer, LOCK_UN);
羊群($ file_pointer,LOCK_UN);
// Use 3 < PHP4.0.2
/ /使用3 <PHP4.0.2
}
}
fclose($file_pointer);
fclose($ file_pointer);
print "data written to file successfully";
打印“写入文件成功数据”;
?gt;
?总吨位;
Hmmm...
嗯...
something different in appending data, yes FSEEK!
东西在追加数据,是FSEEK不同!
It's always good to make sure that the file writing cursor is at the end of the file.
这是一件好事,确保光标在文件写入该文件的末尾。
For doing all this in windows, just escape the file names with a '\'.
在Windows中做这一切,只是拿到了一张文件名'\'。
MISC NOTES ABOUT FLOCK :
船务附注关于羊群:
Flock() locks the file only after the file has been opened.
羊群()后,才锁定该文件已被打开的文件。
So incase at instance A the file was opened and at instance A.5 the lock was obtained.
因此,在该文件是一个实例,在实例A.5节开幕锁柜面获得。
Now the file contents are the contents that were present at instance A and not when the other program finished whatever (appending assumed).
现在,文件内容是内容,是在提出一个实例,而不是在任何其他程序完成(追加假设)。
Hence fseek should always be used not only in appending but also when reading file contents.
因此,应始终使用fseek不仅在阅读时也附加文件的内容。
鲜花(0) 鸡蛋(0)
ljmabcljm 发表于 2014-2-27 23:30:00 | 显示全部楼层
本帖最后由 三T上人 于 2016-7-22 12:28 编辑 <br /><br />回帖是种美德.  

SOSO.CC
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|关于我们|QQ即时充值|站点统计|手机版|小黑屋|百宝箱|留言|咨询|微信订阅|QQ189615688|东南西北人

GMT+8, 2020-8-5 16:27 , Processed in 0.137451 second(s), 35 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表