Home > Backend Development > PHP Tutorial > Sample code for php to read files

Sample code for php to read files

WBOY
Release: 2016-07-25 08:44:24
Original
910 people have browsed it

This php code demonstrates in detail how to open a file, read a file and close a file. In php, you can open a file through fopen, get the file size through the filesize function, fread to read the file content, and fclose to close the file

  1. < html>
  2. Reading a file using PHP
  3. $filename = "/home/user/guest/ tmp.txt";
  4. $file = fopen( $filename, "r" );
  5. if( $file == false )
  6. {
  7. echo ( "Error in opening file" );
  8. exit();
  9. }
  10. $ filesize = filesize( $filename );
  11. $filetext = fread( $file, $filesize );
  12. fclose( $file );
  13. echo ( "File size : $filesize bytes" );
  14. echo ( "" );
  15. ?>
Copy code

php


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template