- @ $ Fp = fopen ("http://bbs.it-home.org/abc123", 'w ');
- If (! $ Fp)
- {
- Echo 'file does not exist ';
- Exit;
- }
- ?>
The @ symbol indicates that PHP will suppress all errors generated by the current function call.
2. PHP needs to read the file after opening the file in PHP. the fgets () function is generally used.
This function can read a row of content from the file each time, and it constantly reads data, the value to the line break or the end symbol EOF the full text.
The fgets () function can only read one row of data. to read all the data of a file, use a circular statement. For example:
- While (! Feof ($ fp ))
- {
- $ Bruce = fgets ($ fp );
- Echo $ bruce;
- }
Here, the feof () function is used to check whether the file ends. The unique parameter of this function is the file pointer (that is, the file opened by $ fp ). Of course, you can also use the readfile () function to read the entire file at a time in PHP. This function includes opening a file, reading a file, outputting it to a browser, and closing the file. For example:
- $ Bruce = readfile ("http://bbs.it-home.org ");
- Echo $ bruce;
- ?>
3. close the file in PHP.
Use the fclose () function to close the file.
2. how to write data to files in PHP
Like PHP reading files, PHP writing files also requires opening files, writing data, and closing files. The above describes how to open and close a file. what is the data written to a file in PHP.
Use the fwrite () function, such as fwrite (file path, write content ):
- $ Bruce = fopen ("http://bbs.it-home.org/", "r ");
- If (! $ Bruce)
- {
- Echo 'file does not exist ';
- Exit;
- }
- While (! Feof ($ bruce ))
- {
- $ Rose = fgets ($ bruce );
- $ James = fopen ("index.htm", "");
- Fwrite ($ james, $ rose );
- Fclose ($ james );
- }
- Fclose ($ bruce );
- ?>
- The jbxue.com contents are generated as local files.
Other useful file functions: file_exists (): Check whether the file exists, return the boolean value filesize (): view the file size, you can directly echo the output unlink (): delete the file, note that PHP does not have the delete function. |