FAQs about file operations in PHP Development

Source: Internet
Author: User

Preface:

PHP supports operations on various types of databases, and also provides rich operation methods for file operations, many of my friends are still operating on files, but sometimes there are still many puzzles and doubts when operating on files, the following are some of the file operations problems I encountered during my daily writing and my friends encountered on the altar.

  Q: How to create a file?

A:

1. Use fopen (name of the file to be created, parameter). Optional values include w, w +, a, and a +.

2. Use exec (echo> file name to be created). In this way, you can use the linux Command touch to create this file.

  Q: Why cannot I create a file?

A:

1. If you use fopen to create a file, is the parameter used correctly?

2. For system permission questions, please ask your WEBMASTER if your FTP directory has write permission

3. For FTP permissions, make sure that you have the write permission in the directory where the file to be written in your php file is located, that is, the other group must have the write permission after your FTP software logs in,
If you do not have the permission, modify the permission and try again.

  Q: How do I read files into an array?

A: use the file function.

  Q: How can I read all files?

A:
 
1. Use fread ($ fp );

2. If your PHP version is> = 4.3.0, you can use file_get_contents ();

  Q: How can I determine whether a file exists?

A: Use file_exists ();

  Q: Why not use fopen () for determination?

A: The reason is that sometimes the data returned by fopen leads us to wrong judgments due to permission issues.

  Q: Why does an error occur when I read a web page?

A:

1. It may be your passing parameter error. when reading the web page, you can only read the page in r mode.

2. Make sure that the WEB page you want to read can be accessed

  Q: How can I obtain the relevant attributes of a file?

A: PHP provides a set of methods to obtain file attributes, such as filemtime (), fileowner (), filegroup (), filectime (), fileatime ()... for more information, see the manual.

  Q: Can I locate the file "cursor" like C after opening the file in PHP?

A: Yes. Use fseek ();

  Q: What should I do if I don't allow others to access this file?

A:

1. You can use other programs to restrict access to file pages.

2. Use flock (). For detailed parameters and usage instructions, see the manual.

  Q: How do I delete the first row of data in a file or specify a row of data?

A:

PHP does not provide such an operation method, but we can use it in combination. The following Code demonstrates that we will delete the third row of data in the test. dat file (more than three rows of data in the test. dat file)

<? Php
$ Filename = test. dat; // defines the operation File
$ Delline = 3; // number of rows to be deleted
If (! File_exsits ($ filename )){
Die (the specified file is not found! Operation interrupted !);
}
$ Farray = file ($ filename); // read file data to the array
For ($ tmpa = 0; $ Tmpa <count ($ farray); $ Tmpa ++ ){
If (strcmp ($ Tmpa + 1, $ delline) = 0 ){
// Determine the row to be deleted
Continue;
}
// Reorganize the data
$ Newfp. = $ farray [$ Tmpa]. \ r \ n;
}
$ Fp = @ fopen ($ filename, a) or die (opening the file $ filename in write mode fails); // open the file in write mode
@ Fputs ($ fp, $ newfp) or die (file write failure );
@ Fclose ($ fp );
?>

The code above demonstrates how to delete a row of files. However, if you take a closer look, you will be provided with reminders about other file operations ~

  Q: When I try to open a non-existent file, how can I prevent the error from being displayed to avoid path leakage !!

A: add the @ symbol before you open the file to block errors, @ is a special symbol for the error message shielding provided by PHP or you can add (usually at the top) error_reporting (0) before this step ); it is not recommended to display all error messages on the page by modifying 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 the ISP modify open_basedir in php. ini for restrictions. The ISP is not recommended to add files such as fopen and file to disable_function.

  Q: Why can't I delete these files after I use PHP to create files ??

A: This is mainly because the files created by PHP belong to the WEB user group, that is, the files created are not from your FTP users !!! The solution to this problem is to use the chmod and unlink methods of the PHP program for processing. We recommend that you remember the chmod File Permission when using PHP to create a file. The recommended value is 777.

  Q: How to Use text files as data warehouses? Some message books and forums all use this!

A: Actually, this mainly uses file, which is a typical example of reading and splitting data with explode.

  Q: How do I change the file name?

A: rename ();

  Q: How to delete an object?

A: unlink (); exec (del (rm-vf) filename );

Note: rm-vf is used in linux.

  Q: How do I clear files?

A: Use fopen (filename, w); or exec (echo> filename );

  Q: How to edit the file content?

A: I remember I answered a previous question about deleting a file. In fact, you can replace the variable on the basis of the deleted content. I hope you can look up and change the continue above to replace the variable data.

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.