PHP operation File Some FAQ summary _php tutorial

Source: Internet
Author: User
Tags ftp login
Q: How do I create a new file?
A: 1, use fopen ("file name to establish", "parameter"), parameter optional w,w+,a,a+
2. Use EXEC ("echo ' > file name to be established"), so 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?
A: 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?
A: Use the file
Q: How do I read all the files?
Answer: 1, use 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 (); Why not use fopen () to judge? 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?
A: 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 () ... Use in detail
Please refer to the manual.
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.
A: 1, you can use other aspects of the program to restrict the user access to file Operation 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?
A: PHP does not provide this method of operation, but we can use the combination, the following code demonstrates that we will delete the file "Test.dat" in the third row of data (TEST.DATW
More than three lines of data in the file)
Copy CodeThe code is as follows:
$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]. " \ r \ n ";
}
$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 that you want to manipulate, to block the display of all error messages in the page
A deprecated method is to modify PHP.ini (except 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 deprecated ISP setting is to add file operations such as Fopen,file 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 PHP program Chmod,unlink and other ways to handle, it is recommended that the user when using PHP to create files
Remember 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/319787.html www.bkjia.com true http://www.bkjia.com/PHPjc/319787.html techarticle Q: How do I create a new file? A: 1, using fopen ("file name to be established", "parameters"), parameter optional w,w+,a,a+ 2, use EXEC ("echo" to establish the filename "), this is the use of the system ...

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