Preface:
In PHP, the operation of various databases is supported, and the operation of the file also has a rich operation method. Many friends are still based on file operations.
However, sometimes there are many confuses and doubts when the document is operated. The following is what I encountered in the daily preparation process and what I encountered at the altar.
Some questions collection.
Q: How to create a new file?
A: 1. Use FOPEN ("File Name to build", "parameter"), parameter options W, W , A, A
2, use EXEC ("Echo ''" to be established ")); this is to use the system mode to establish this file, you can also use Touch Linux command to build
Q: Why can't I create a file?
A: 1. If you use the FOPEN to build a file, do you use the correct use of parameters?
2, system permission issues, please ask your WebMaster your FTP directory to write permissions
3. FTP permission issues, you have to confirm that your PHP file is written by the directory where you want to write, that is, your FTP software logs in Other group to write this permission,
If you don't have the right to modify your permissions, try
Q: How do I read the file into an array?
A: Using File
Q: How do I read all the files?
A: 1. Use FREAD ($ FP);
2, if your PHP version> = 4.3.0 can be used to use file_get_contents ();
Q: How do I determine if the file exists?
A: Use file_exists (); why not use fopen () to determine? The reason is sometimes because the permissions have caused the data returned by the FOPEN to guide us the wrong judgment.
Q: Why did you make an error when I read a web page?
A: 1, maybe your pass parameters wrong, you can only read the page when reading the web page.
2, make sure you want to read the web page to access
Q: How can I get the relevant properties of the file?
A: PHP provides a group of methods for obtaining file properties, such as FilemTime (), Fileowner (), FileGroup (), Filectime (), FileATIME () ... Detailed use
See the manual.
Q: Can the PHP open the file "Cursor" in the file?
A: Yes, use fseek ();
Q: What should I do if I don't allow other people to access this file when I have access to the file?
A: 1, you can use other aspects to limit the page of the user access file operation
2, use flock (); detailed parameters, please refer to the manual
Q: How do I delete the first line in the file or specify a line of data?
A: PHP does not provide this way of operation, but we can use the following code demonstrate that we will delete the third line of data in the file "Test.dat" (Test.datw
Data in the file more than three lines)
PHP
$ filename = "test.dat"; // Define the operation file
$ Delline = 3; // The number of rows to delete
IF (! File_exsits) {
DIE ("The specified file is not discovered! Operation interrupt!");
}
$ farray = file ($ filename); // read file data to an array
For ($ TMPA = 0; $ TMPA IF (STRCMP ($ TMPA 1, $ Delline) == 0) {// Judgment Deleted Row CONTINUE; } // Rearranged data $ newfp. = $ farray [$ TMPA]. "/ r / n"; } $ fp = @ fopen ($ filename, "a") or DIE ("Write mode open file $ filename failed"); // We write files in writing @fputs ($ fp, $ newfp) or Die ("file writing failed"; @fclose ($ fp); ?> The above code is deleted to delete a line of files, but if you look carefully, it is actually given you a reminder of other file operations ~ Q: When I tried to open a file that did not exist, I didn't let the error showed it to avoid my path to disclose! ! A: Add @ symbol to block errors before you want to open the file, @ is a dedicated symbol of the error information shield provided by PHP Or you can increase before this step (usually on page) Error_Reporting (0); display to block all error messages in the page A unkreated method is to modify php.ini (except ISP) Q: I am using a virtual host, how do I prevent other users from stealing my data? A: It is recommended that ISP modifies the Open_BASEDIR in php.ini to limit, The unhefetried ISP settings add file operations such as FOPEN, FILE to disable_function. Q: Why did I use a PHP to build a file I want to delete these files can't be deleted? ? A: It is mainly because PHP is owned by the web user group, which is also the created file, not your FTP user! ! ! The solution to this problem is that the use of PHP programs ChMOD, UNLINK, etc., it is recommended that users use PHP to build files. Remember CHMOD file permissions, it is recommended to be 777 Q: How do I use text files as a data warehouse? Some message books, the forums are used to use this! A: In fact, this mainly uses File, combined with Explode to read and split typical examples. Q: How do I change the file name? A: rename (); Q: How do I delete a file? A: unlink (); Exec ("DEL (RM -VF) filename"); Note: RM -VF is used under Linux Q: How to empty a file? A: Using Fopen (FileName, "W"); or Exec ("Echo ''> FileName"); Q: How do I edit the contents of the file? A: I remember that I have answered a deleted file content, in fact, the editing content can be replaced on the basis of deleting content. I hope you can find it up, and you can modify my continue above to replace variable data :)