PHP example of obtaining (reading) local and remote file content-php Tutorial

Source: Internet
Author: User
Tags php example
PHP get (read) local file and remote file content example

  1. $ File = 'jbxue. com. php ';
  2. // Remote access is not supported
  3. $ Fso = fopen ($ file, 'r ');
  4. Echo $ data = fread ($ fso, filesize ($ file ));
  5. Fclose ($ fso );
  6. ?>

Fopen () binds the name resource specified by file to a stream. filesize: the number of bytes of the file size. If an error occurs, FALSE is returned. note: Because the integer type of PHP is signed, and most platforms use 32-bit integers, the filesize () function may return unexpected results when encountering a file larger than 2 GB. for files between 2 GB and 4 GB, sprintf ("% u", filesize ($ file) can be used to overcome this problem. fread () reads a maximum of length bytes from the file pointer handle. this function reads the length of several bytes, or when it reaches the EOF, or (for network streams) when a package is available, it stops reading files, depending on the first situation. note: Usage of earlier versions! We recommend that you use file_get_contents for php5.

Example 2,

  1. $ File = 'jbxue. com. php ';
  2. // Remote support
  3. $ File = 'http: // bbs.it-home.org ';//
  4. Echo $ data = implode ('', file ($ file ));
  5. ?>

File-read the entire file into an array to read binary files.

Example 3,

  1. $ File = 'http: // bbs.it-home.org ';
  2. Echo file_get_contents ($ file );
  3. ?>

File_get_contents -- read the entire file into a string indicating that string file_get_contents (string filename [, int use_include_path [, resource context]) is the same as file (), except for file_get_contents () returns the object as a string. the file_get_contents () function is used to read the file content into a string. if the operating system supports it, the memory ing technology will be used to enhance the performance.

I have shared the code for getting remote files in three php examples. I hope it will be helpful to you.

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.