Questions and answers about file operation in the production of PHP dynamic website

Source: Internet
Author: User
Tags chmod ftp ftp login ini php file php dynamic website variable file permissions

Preface:

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, the use of fopen (to establish the file name, parameters), optional parameters w,w+,a,a+

2, using the EXEC (echo ' > file name to be established); This is the way to create 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)

Define an action file $filename =test.dat;//
$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 ($filename failure to open the file by writing);//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 screen 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) before the step to be manipulated; The display that masks all the error messages in the page an deprecated method is to modify the php.ini (excluding ISPs).

  Q: I am using a virtual host, how can I prevent other users from stealing my data?

A: It is recommended that the ISP modify the Open_basedir in php.ini to restrict, the ISP setting that does not recommend is to add fopen,file file operation to join 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 the variable data.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.