PHP Fread Read files

Source: Internet
Author: User
Tags fread php script

PHP read the file , Fread is used to read the contents of the file opened with fopen, let's take a look at the fread and gets instance tutorial.
I apologize simply by taking the point of actually getting to your profile file. In this lesson we will teach you how to read data from files using a variety of PHP functions.

PHP-Open File: Read
In the information file we can read, we must use the feature to open and open the file for reading. Here the code is changed to open file, we build the lessons in PHP file collection.

$myFile = "TestFile.txt";
$fh = fopen ($myFile, ' R ');

The document we built in the past is known as "TestFile.txt". Your PHP script, you should write live in the same directory "Text.txt". The following is the contents of the file from the file collection.

PHP-File reading: Fread features
The Fread function is a staple to get data from a file. This feature requires a file handle that we have and an integer that tells the function how much data, in bytes, it is read.

A word, equal to one byte. If you want to read the first five-year character then you can use integers for five years.

$myFile = "TestFile.txt";
$fh = fopen ($myFile, ' R ');
$theData = Fread ($fh, 5);
Fclose ($FH);
Echo $theData;

The first five-year character TestFile.txt from now archives stored in $ thedata. You can repeat this string, $ thedata, or write to another file.

If you want to read all the data files, then you need to get the size of the file. The file size function returns the length of the file, in bytes, which is what we need! The file's functional requirements for the file name will be sized up.

$myFile = "TestFile.txt";
$fh = fopen ($myFile, ' R ');
$theData = Fread ($fh, FileSize ($myFile));
Fclos E ($FH);
Echo $theData;

php-file reading: Gets function
PHP can also read the data line time from a file to get the feature. This can or may not be useful to you, programmer. If you have your data separated with the new line then you can read a portion of the data at the time and get the function.

Fortunately our "testFile.txt" file is separate from the new production line, we can take advantage of this feature.

$myFile = "TestFile.txt";
$fh = fopen ($myFile, ' R '),
$theData = fgets ($fh),
fclose ($FH);
Echo $ Thedata;
www.111cn.net/phper/php.html

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.