In this tutorial, You will know about how to Reading File Data in PHP
Here are the simplest 4 methods with example to read the file.
1. fread()
2. fgets()
3. file()
4. file_get_contents()
Let’s get in the detail.
the number in green, followed by the line contents, and repeats until all lines have been printed.
Please share views in comment and read our other PHP tutorial blogsReading File Data
If you want to read the file from the server using PHP, there are many ways to do that.Here are the simplest 4 methods with example to read the file.
1. fread()
2. fgets()
3. file()
4. file_get_contents()
Let’s get in the detail.
1. Using fread () in PHP
You can use fread () method when you want to read the file data and return it to a string. You’ll be able to specify how many bytes to read, but 8192 is the maximum. If you choose to use this method, you must be sure to open the file 1st.<?php $myfile = "myfile.txt"; //Select the file $handle = fopen($myfile, 'r'); // Open the file $data = fread($handle, 512); // Read the file. fclose($handle); // Close the file print $data; // Print the data ?>This code OPens the file myfile.txt from the server location and content from that file will be stored in to $data variable as a string and then print the content. So easy!
2. Using file_get_contents () in PHP
file_get_contents () is similar to fread () in that it returns the whole contents of the file to a one string, however it can be done in a single line and performs higher then fread ().<?php $file = file_get_contents ('myfile.txt'); echo $file; ?>This single line code fetches the data from myfile.text.
3. Using fgets () in PHP
fgets () is used to browse the content from a file one line at a time starting from the pointer. It defaults to only reading the first 1024 bytes of a line, however, you can set this variable higher or lower if you want. If your file isn’t separated with line breaks, this is not the correct function to use.<?php $myfile = "myfile.txt"; //Select the file $handle = fopen($myfile, 'r'); //Open the file while (!feof($handle)) // Fetch the data till end of file { $data = fgets($handle, 256); //get the content and storing to $data print $data; // print the data print "<p>"; } fclose($handle); //close the file ?>
4. Using file () in PHP
File () is comparable to fgets in that it reads the content one line at a time, but it returns it all at once into an array. The array contains the line number, and the data corresponding to each line number starting with zero. If you would like to do over than simply echo the data, having it in an array will offer a lot of rather more flexibility creating file () more helpful than fgets ().<?php $lines = file('myfile.txt'); foreach ($lines as $line_num => $line) { print "<font color=green>Line #{$line_num}</font> : " . $line . "<br />\n"; } ?>
How it works: first is get the contents of myfile.txt into the array called $lines. It then enters into a loop which prints every line
the number in green, followed by the line contents, and repeats until all lines have been printed.
Take an audit of your uses for a desktop computer before buying one.satta king play bazaar This is important so that you can really understand how much power you really need. More power means more money. play bazaar satta king If super powerful desktop won't be of any real use to you, why shell out the additional funds?
ReplyDelete