WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 File Open/Read/Close


In this section we will show you how to open, read, and close a record on the server.


PHP Open File - fopen()

A better strategy to open records is with the fopen() work. This capacity gives you more alternatives than the readfile() function.

We will utilize the content document, "webdictionary.txt", amid the lessons:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The first parameter of fopen() contains the name of the document to be opened and the second parameter determines in which mode the document ought to be opened. The accompanying precedent additionally produces a message if the fopen() work can't open the predetermined file:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Run model »

Tip: The fread() and the fclose() capacities will be clarified below.

The record might be opened in one of the accompanying modes:

Modes Description
r Open a record for read only. Document pointer begins toward the start of the file
w Open a record for compose only. Deletes the substance of the document or makes another record on the off chance that it doesn't exist. Document pointer begins toward the start of the file
a Open a document for compose only. The current information in record is safeguarded. Document pointer begins toward the finish of the record. Makes another record if the record doesn't exist
x Creates another record for compose only. Returns FALSE and a mistake if document as of now exists
r+ Open a record for read/write. Record pointer begins toward the start of the file
w+ Open a record for read/write. Eradicates the substance of the record or makes another document in the event that it doesn't exist. Record pointer begins toward the start of the file
a+ Open a record for read/write. The current information in record is protected. Record pointer begins toward the finish of the document. Makes another document if the document doesn't exist
x+ Creates another document for read/write. Returns FALSE and a mistake if record as of now exists

PHP Read File - fread()

The fread() work peruses from an open file.

The first parameter of fread() contains the name of the document to peruse from and the second parameter indicates the greatest number of bytes to read.

The following PHP code peruses the "webdictionary.txt" record to the end:

fread($myfile,filesize("webdictionary.txt"));

PHP Close File - fclose()

The fclose() work is utilized to close an open file.

The fclose() requires the name of the record (or a variable that holds the filename) we need to close:

<?php
$myfile = fopen("webdictionary.txt", "r");
//some code to be executed....
fclose($myfile);
?>

PHP Read Single Line - fgets()

The fgets() work is utilized to peruse a solitary line from a file.

The precedent beneath yields the main line of the "webdictionary.txt" file:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Run precedent »

PHP Check End-Of-File - feof()

The feof() work checks if the "end-of-file" (EOF) has been reached.

The feof() work is helpful for circling through information of obscure length.

The model beneath peruses the "webdictionary.txt" document line by line, until end-of-record is reached:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
//Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
Run model »

PHP Read Single Character - fgetc()

The fgetc() work is utilized to peruse a solitary character from a file.

The model beneath peruses the "webdictionary.txt" document character by character, until end-of-document is reached:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
//Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>
Run precedent »






Welookups is optimized for learning.Copy right 2018 - 2019 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.

welookups.com