How to read data from a file stored in XAMPP webserver using PHP ?
Last Updated :
01 Dec, 2020
We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem.
File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc
fopen() function: The fopen() function in PHP is an inbuilt function which is used to open a file or a URL. It is used to bind a resource to a steam using a specific filename. The filename and mode to be checked are sent as parameters to the fopen() function and it returns a file pointer resource if a match is found and a False on failure. The error output can be hidden by adding an ‘@’ in front of the function name.
Syntax:
fopen('filename', filemode)
Here, file name is name of the file and file mode includes read(r) mode,
write(w) and binary(b) mode etc.
- fopen($geek, r) — Here we are opening geek file in read mode.
- fopen($geek, r+) — Here we are opening geek file in read and write mode.
- fopen($geek, w) — Here we are opening geek file in write mode.
- fopen($geek, w+) — Here we are opening geek file in read and write mode.
- fopen($geek, b) — Here we are opening geek file in read and write mode.
Requirements:
XAMPP web server — If you have not installed XAMPP/WAMP web server then please install it by using the following steps:
Link to install: https://www.apachefriends.org/download.html
Start the XAMPP Server
Open the notepad and type the below code:
PHP
<?php
// File to be read
$file = "./welcome.txt";
// Opening file
$f = fopen($file, "r") or
exit("Unable to open file!");
// Read file line by line until
// the end of file (feof)
while(!feof($f)) {
echo fgets($f)."<br />";
}
// Closing file
fclose($f);
?>
Data in welcome.txt file are:
GEEKS FOR GEEKS IS BEST FOR COMPUTER SCIENCE
Place these two files in folder (Path is show here)
Path
Running the Script Type the following URL in the browser:
localhost/gfg/1.php
Output:
Similar Reads
How to execute PHP Script in Website using XAMPP webserver ? First we have to install the XAMPP/WAMP webserver in our system. Please follow the link to download and install XAMPP/WAMP server. Link https://www.apachefriends.org/download.html After successful installation, the steps we should follow are- Open XAMPP control panel, if you want to link database to
2 min read
PHP program to fetch data from localhost server database using XAMPP In this article, we will see how we can display the records by fetching them from the MySQL database using PHP. Approach: Make sure you have an XAMPP server or WAMP server installed on your machine. In this article, we will be using the XAMPP server.XAMPP is a free and open-source cross-platform web
4 min read
How to Export data to CSV file from Database using XAMPP ? In this article, we are going to load the data present in the database (MySQL) into CSV file. Display the data in the database and also export that data into CSV file. We are using XAMPP tool to store locally in a database. XAMPP stands for  cross-platform, Apache, MySQL, PHP, and Perl. It is among
3 min read
How to display XML data in web page using PHP ? In this article, we are going to display data present in an XML file on a web page using PHP through the XAMPP server. PHP is a server-side scripting language that is mainly for processing web data. The XML stands for an extensible markup language. Requirements: XAMPP server Syntax: <root>
2 min read
How to post data using file_get_contents in PHP ? The file_get_contents() function in PHP is used to read the contents of a file and make HTTP requests using GET and get HTTP responses using POST methods. The HTTP POST request can be made using the $context parameter of the file_get_contents() function, which posts the specified data to the URL spe
3 min read