Home PHP Filesystem fgetss() PHP fgetss() Function Definition In PHP, fgetss() used to read one line from a file and strip the HTML and PHP tags from it. The function was deprecated in PHP 7.3 and removed completely in PHP 8.0.
If you still see fgetss() in old code, it will crash on PHP 8 with an undefined function error. The replacement is easy: read the line with fgets() and clean it with strip_tags().
Syntax Call fgetss() with the values you need. Start with this line.
$line = strip_tags(fgets($handle)); Call What it does strip_tags(fgets($handle))The modern replacement for fgetss($handle). Reads a line and removes all tags. strip_tags(fgets($handle), '<b>')Replaces fgetss($handle, null, '<b>'). Keeps the b tags and removes the rest. function_exists('fgetss')Returns false on PHP 8, which tells you the function is gone.
fgetss() was deprecated in PHP 7.3 and removed in PHP 8.0. Do not use it in new code. Replace every call with strip_tags(fgets($handle)). The $allowable_tags argument maps to the second argument of strip_tags().
Library declarations fgetss(resource $stream, ?int $length = null, string $allowable_tags = ?): string|false Parameters Parameter Description stream Required (old versions only). An open file stream, such as the one returned by fopen(). length Optional (old versions only). The number of bytes to read, as in fgets(). allowable_tags Optional (old versions only). Tags that should not be stripped. In strip_tags() this is the second argument.
Return Value Return Value Description string In PHP 7 and earlier, the line with HTML and PHP tags removed. false In PHP 7 and earlier, returned at the end of the file or on failure. In PHP 8 the function no longer exists.
Important
fgetss() no longer exists in PHP 8.0 and later. Calling it throws a fatal Error: Call to undefined function fgetss(). Note that fgets() can return false at the end of the file, so check it before passing it to strip_tags(), which expects a string.
PHP Examples 1. Migrate to strip_tags(fgets()) 2. The Function Is Gone in PHP 8 3. Keep Some Tags Example 1 — Migrate to strip_tags(fgets()) Read a line with fgets() and remove its tags with strip_tags(). This does what fgetss() used to do.
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetss.txt';
file_put_contents($path, '<b>Bold</b> <i>text</i>' . PHP_EOL);
$handle = fopen($path, 'r');
$line = fgets($handle);
if ($line !== false) {
echo trim(strip_tags($line)), PHP_EOL;
}
fclose($handle);
unlink($path); How It Works The file contains one line with b and i HTML tags. fgets() reads the line with its tags still in place. strip_tags() removes every tag and keeps only the text between them. The if check makes sure fgets() did not return false, and trim() removes the line break. Example 2 — The Function Is Gone in PHP 8 On PHP 8 and later, calling fgetss() throws an Error. Catch it to see the message, or use function_exists() to check first.
<?php
echo 'fgetss exists: ', var_export(function_exists('fgetss'), true), PHP_EOL;
try {
$line = fgetss(STDIN);
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
} fgetss exists: false
Call to undefined function fgetss() How It Works function_exists() returns false because PHP 8 removed fgetss(). Calling the missing function throws an Error object. Error is the right class to catch, because this is not an Exception. getMessage() prints the text of the error so you can recognize it in your logs. Frequently Asked Questions Was fgetss() removed from PHP? Yes. fgetss() was deprecated in PHP 7.3 and removed in PHP 8.0. Calling it on PHP 8 throws an Error saying the function is undefined.
What should I use instead of fgetss()? Use strip_tags(fgets($handle)). fgets() reads the line and strip_tags() removes the HTML and PHP tags from it.
How do I keep some tags like fgetss() allowed? Pass the allowed tags as the second argument of strip_tags(), for example strip_tags($line, '<b><i>').
Why does my old script show Call to undefined function fgetss()? Your server was upgraded to PHP 8 or later, where the function no longer exists. Replace each fgetss() call with strip_tags(fgets()).
Is strip_tags() enough to make output safe? No. strip_tags() removes tags but it is not a full security filter. When you print user text in HTML, also use htmlspecialchars().
Next: file() file() reads a whole file and returns it as an array, with one item for each line. It is t.
file() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion