_php tutorials for troubleshooting file operations in PHP development

Source: Internet
Author: User
Tags ftp login
Author: Yang Zongwei
Objective:

PHP in the operation of various types of database support, the operation of the file also has a very rich operation method, many friends now operation is still based on the file operation but sometimes in the operation of the file there is still a lot of confusion and doubt, Here are some of the issues I encountered during my daily writing and the questions that my friends on the altar have encountered about file manipulation.

Q: How do I create a new file?

For:

1. Use fopen ("file name to be established", "parameters"), parameter optional w,w+,a,a+

2. Use EXEC ("echo > file name to be established"), so that you can use the system to build this file, you may also use Touch this Linux command to build

Q: Why can't I create 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 problem, you want to confirm that your PHP file to write the directory to be written to have permission to write, that is, your FTP software login after the other group to have to write this permission,

If you do not modify permissions, 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 do 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 a permission problem, the data returned by fopen leads us to the wrong judgment.

Q: Why is there an error when I read a Web page?

For:

1, may be your pass parameter is wrong, when reading the Web page you can only use the R method to read the page

2. Make sure that the Web page you want to read is accessible

  
Q: How do I get properties related to a file?

A: PHP provides a set of methods to obtain file attributes, such as Filemtime (), Fileowner (), filegroup (), Filectime (), Fileatime () ... Please refer to the manual for detailed use.

Q: Can PHP be positioned as a file "cursor" like C after opening a file?

Answer: Yes, use fseek ();

Q: I want to not allow other people to access this file while accessing the file.

For:

1, you can use other aspects of the program to restrict the user access to file operation of the page

2, using Flock (), detailed parameters and how to use see the manual

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

For:

PHP does not provide such a method of 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 lines of data)

  

$filename = "Test.dat";//define Action file

$delline = 3; Number of rows to delete

if (!file_exsits ($filename)) {

Die ("The specified file is not found! Operation interrupted! ");

}

$farray =file ($filename);//Read the file data into the array

for ($tmpa =0; $Tmpa
if (strcmp ($Tmpa +1, $delline) ==0) {

Determine the deleted row

Continue

}

Re-organized data

$NEWFP. = $farray [$Tmpa]. "";

}

$fp = @fopen ($filename, "a") or Die ("Write Open file $filename failed");//We open the file in the 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, in fact, also provides you with other file operation related reminders ~

Q: When I try to open a nonexistent file, how do 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 the 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; An deprecated way to block all error messages in a page is to modify php.ini (except for the 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 the Open_basedir in php.ini to restrict, the ISP setting is not recommended is to add fopen,file and other file operations to Disable_function.

Q: Why do I use PHP to build files after I ftp login to delete these files cannot be deleted??

A: The main reason is that the files that are created by PHP belong to the Web user group, which is the created file, not your FTP user!!! The solution to this problem is to use the PHP program Chmod,unlink and other means of processing, it is recommended that users in PHP to create files when the chmod file permissions, recommended for 777

Q: How do I use a text file as a data warehouse? Some of the message book, Forum and the like are using this ah!

A: In fact, this is mainly the use of file, combined with explode for data reading and segmentation of the typical example.

Q: How do I change the file name?

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?

A: Use fopen (filename, "w"), or EXEC ("echo > FileName");

Q: How do I edit the contents of a file?

A: I remember answering a previous answer to delete the contents of the file, in fact, the content of the editor on the basis of the deletion, the variable can be replaced. I hope you can look up and change the continue above to replace the variable data.

http://www.bkjia.com/PHPjc/532668.html www.bkjia.com true http://www.bkjia.com/PHPjc/532668.html techarticle Author: Yang Zongwei preface: PHP in the operation of various types of database support, the operation of the file also has a very rich operation method, many friends are now operating or file-based ...

  • 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.