PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
fgets()
and fgetss()
are two functions in PHP that allow you to read from an open file. The difference between the two lies in how they handle HTML and PHP tags in the file's content.
fgets():
The fgets()
function reads a line from an open file.
Syntax:
fgets(resource $handle [, int $length]) : string|false
$handle
: The file pointer must be valid and point to a file successfully opened by fopen()
or fsockopen()
.$length
: Reading ends when length - 1 bytes have been read, a newline is found, or EOF is reached, whichever comes first.Example:
<?php $file = fopen("testfile.txt", "r"); if ($file) { while (($line = fgets($file)) !== false) { echo $line . "<br>"; } fclose($file); } else { echo "Error opening the file."; } ?>
fgetss():
The fgetss()
function reads a line from an open file and strips HTML and PHP tags.
Syntax:
fgetss(resource $handle [, int $length [, string $allowable_tags]]) : string|false
$handle
: The file pointer must be valid and point to a file successfully opened by fopen()
or fsockopen()
.$length
: Reading ends when length - 1 bytes have been read, a newline is found, or EOF is reached, whichever comes first.$allowable_tags
: You can use this optional third parameter to specify tags which should not be stripped.Example:
<?php $file = fopen("testfile.txt", "r"); if ($file) { while (($line = fgetss($file)) !== false) { echo $line . "<br>"; } fclose($file); } else { echo "Error opening the file."; } ?>
Please note that as of PHP 7.3.0, fgetss()
function has been deprecated. If you need to read from a file and strip tags, consider using fgets()
in combination with strip_tags()
.
fgetss()
function in PHP:
fgetss()
reads a line from a file and strips HTML and PHP tags.$file = fopen("example.html", "r"); $line = fgetss($file); fclose($file);
PHP read file line by line example:
$file = fopen("example.txt", "r"); while ($line = fgets($file)) { // Process each line } fclose($file);
File handling in PHP with fgets()
:
fgets()
reads a line from a file.$file = fopen("example.txt", "r"); $line = fgets($file); fclose($file);
PHP fgets()
vs fgetss()
differences:
fgets()
reads a line without filtering HTML and PHP tags, while fgetss()
does HTML and PHP tag filtering.// Using fgets() $file = fopen("example.html", "r"); $line = fgets($file); fclose($file); // Using fgetss() $file = fopen("example.html", "r"); $line = fgetss($file); fclose($file);
Secure file reading in PHP fgetss()
:
fgetss()
for secure file reading by filtering out HTML and PHP tags.$file = fopen("securefile.html", "r"); $line = fgetss($file); fclose($file);
PHP read text file line by line:
fgets()
.$file = fopen("textfile.txt", "r"); while ($line = fgets($file)) { // Process each line } fclose($file);
PHP fgetss()
for HTML and script tag filtering:
fgetss()
to filter HTML and script tags while reading a file.$file = fopen("securefile.html", "r"); $line = fgetss($file); fclose($file);
PHP file reading functions comparison:
// Using fgets() $file1 = fopen("example.txt", "r"); $line1 = fgets($file1); fclose($file1); // Using fgetss() $file2 = fopen("example.html", "r"); $line2 = fgetss($file2); fclose($file2);