PHP Operation File FAQ
Objective:
PHP for various types of database operations have support, the operation of the file also has a very rich way to operate, many friends are now operating or based on file operations but sometimes in the operation of documents there are still a lot of confusion and doubt, The following are some of the questions I encountered during my daily writing and some of my friends ' problems with file operation.
Q: How do I create a new file?
For:
1. Use fopen ("filename to be established", "parameters"), optional w,w+,a,a+
2. Use EXEC ("echo" > file name to be established); This is the way to build this file using the system, you can also use the touch of the Linux command to build
Q: Why can't I build a file?
For:
1, if you use the fopen to establish the file, whether the correct use of parameters
2, System permissions issues, please ask your webmaster your FTP directory has write permission
3, FTP permissions, you want to confirm your PHP file to write files directory to have write permission, that is, your FTP software landing after the other group to have to write this permission,
If you do not modify permissions after you try
Q: How do I read a file into an array?
Answer: Use the file function
Q: How do I read all the files?
For:
1, the use of fread ($FP);
2, if your PHP version >=4.3.0 words can use file_get_contents ();
Q: How can I tell if a file exists?
Answer: Use File_exists ();
Ask again: Why not use fopen () to judge?
A: The reason is that sometimes because of permission problems, the data returned by fopen leads us to the wrong judgment.
Q: Why did I go wrong when I read a Web page?
For:
1, may be your pass parameter error, when reading the Web page you can only use the R way to read the page
2. Make sure that the Web page you want to read can be accessed
Q: How can I get the related properties of a file?
A: PHP provides a set of methods for obtaining file properties, such as Filemtime (), Fileowner (), filegroup (), Filectime (), Fileatime () ... Please refer to the manual for detailed use.
Q: Can php file "cursor" positioning like C after opening a file?
Answer: Yes, use fseek ();
Q: What if I want to not allow others to access the file while accessing the file?
For:
1, you can use other aspects of the program to restrict user access to File Operations page
2, the use of flock (), detailed parameters and the use of methods refer to the manual
Q: How do I delete the first line within a file, or specify a row of data?
For:
PHP does not provide such an operation, but we can use the combination, the following code shows that we will delete the file "Test.dat" in the third row of data (Test.dat file more than three rows of data)
<?php
$filename = "Test.dat";//define Action file
$delline = 3; Number of rows to delete
if (!file_exsits ($filename)) {
Die ("Specified file not found!") Operation interrupted! ");
}
$farray =file ($filename);//Read file data to array
for ($tmpa =0; $Tmpa <count ($farray); $Tmpa + +) {
if (strcmp ($Tmpa +1, $delline) ==0) {
To determine which rows are deleted
Continue
}
The data after the reorganization
$NEWFP. = $farray [$Tmpa]. " \ r \ n ";
}
$fp = @fopen ($filename, "a") or Die ("write-mode Open file $filename failed");//We open the file in a written way
@fputs ($FP, $NEWFP) or Die ("file write Failed");
@fclose ($FP);
?>
The above code shows the deletion of a row of files, but if you look carefully, it also provides you with other documents related to the operation of the reminder ~
Q: When I try to open a nonexistent file, how can I not let the error show to avoid my path leakage!!
A: Add the @ symbol to mask the error before you open the file, @ is a special symbol for the error message screen provided by PHP
Or you can add (usually at the top of the page) error_reporting (0) in front of the steps you want to manipulate, and screen for all error messages on the page.
One method that is not recommended is to modify the php.ini (except ISP)
Q: I am using a virtual host, how can I prevent other users from stealing my data?
A: It is recommended that ISPs modify the Open_basedir in php.ini to restrict
The ISP setting that is not recommended is to add file operations such as Fopen,file to the disable_function.
Q: Why do I use PHP to create files after I ftp login to delete these files can not be deleted??
A: The main reason is that the file is built by PHP to the Web user group, that is, the establishment of the file, not your FTP users!!!
The solution to this problem is, the use of PHP program Chmod,unlink and other ways to deal with, we recommend that users use PHP to create files when remember chmod file permissions, recommended 777
Q: How do I use a text file as a data warehouse? Some message book, forum and so on are all use this AH!
A: In fact, this is the main use of file, combined with explode data reading and segmentation of a typical example.
Q: How do I change the filename?
Answer: rename ();
Q: How do I delete a file?
Answer: unlink (); EXEC ("del (RM-VF) filename");
Note: RM-VF is used under Linux
Q: How do I empty a file?
Answer: Use fopen (filename, "w"), or EXEC ("echo" > FileName);
Q: How do I edit the contents of a file?
A: I remember replying to a previous answer to delete the contents of the file, in fact, the editing content on the basis of the deletion of content, to replace the variable on it.
I hope you can look up and change my continue to replace variable data: