FileSystem Function in PHP: PART 11

Introduction

In this article I describe the PHP FileSystem functions ftell, ftruncate, fwrite and glob. To learn some other FileSystem functions, go to:

  1. FileSystem Function in PHP: PART 1
  2. FileSystem Function in PHP: PART 2
  3. FileSystem Function in PHP: PART 3
  4. FileSystem Function in PHP: PART 4
  5. FileSystem Function in PHP: PART 5
  6. FileSystem Function in PHP: PART 6
  7. FileSystem Function in PHP: PART 7
  8. FileSystem Function in PHP: PART 8
  9. FileSystem Function in PHP: PART 9
  10. FileSystem Function in PHP: PART 10

PHP ftell() Function

The PHP FileSystem ftell function is used to fetch the current position of the file pointer of an open file and it returns the current position in an open file.

Syntax

fread(file)

Parameters in ftell function

The parameter of the function is:

Parameter Description
file It specifies the open file to check.

Example of ftell function

An example of the function is:


<?
php
$file = fopen("test.txt","r
");

echo ftell($file);
fseek($file,"
15");
// print current position again
echo "<
br />" . ftell($file);
fclose($file);
?>


Output

ftell-function-in-php.jpg

PHP ftruncate() Function

The PHP FileSystem ftruncate function is used to truncates a file to a specified length and it returns true on success or false on failure.

Syntax

fread(file,size)

Parameters in ftruncate function

The parameter of the function are:

Parameter Description
file It specifies the open file to truncate.
size It specifies the new file size.

Example of ftruncate function

An example of the function is:

<?php
echo
filesize("test.txt")."</br>";
$file = fopen("test.txt", "a+
");

ftruncate($file,50);
fclose($file);
//Clear cache and check filesize again
clearstatcache();
echo filesize("
test.txt");
?>

 
Output

ftruncate-function-in-php.jpg

PHP fwrite() Function

The PHP FileSystem fwrite is used to write to an open file and it returns number of bytes on success or false on failure.

Syntax

fread(file,string, length)

Parameters in fwrite function

The parameters of the function are:

Parameter Description
file It specifies the open file to truncate.
string It specifies the string to the open file.
length It specifies the maximum number of bytes to write.

Example of fwrite function

An example of the function is:


<?
php
$fileHandler = fopen("test.txt", "w
");

// Write to the file
fwrite($fileHandler, "
Welcome at MCNSolution!");
fclose($fileHandler);
// Access the file data and echo it
$data = file_get_contents("
test.txt");
echo $data;
?>

 
Output

fwrite-function-in-php.jpg

PHP glob() Function

The PHP FileSystem glob function returns an array of filenames or directories matching a specified pattern.

Syntax

glob(pattern,flag)

Parameters in fwrite function

The parameters of the function are:

Parameter Description
pattern It specifies the pattern to search for.
flag It specifies special settings and the possible values are:
  • GLOB_MARK - Adds a slash to each item returned.
  • GLOB_NOSORT - Return files as they appear in the directory.
  • GLOB_NOCHECK - Returns the search pattern if no match were found.
  • GLOB_NOESCAPE - Backslashes do not quote metacharacters.
  • GLOB_BRACE - Expands {a,b,c} to match "a", "b", or "c".
  • GLOB_ONLYDIR - Return only directories that match the pattern.
  • GLOB_ERR - Stop on errors.

Example of glob function

An example of the function is:


<?
php
print_r
(glob("*.txt"));
?>

 
Output

glob-function-in-php.jpg 


Similar Articles