FileSystem Function in PHP: Part 17

Introduction

In this article I describe the PHP FileSystem functions rewind, rmdir, set_file_buffer, stat and symlink. 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
  11. FileSystem Function in PHP: PART 11
  12. FileSystem Function in PHP: PART 12
  13. FileSystem Function in PHP: PART 13
  14. FileSystem Function in PHP: PART 14
  15. FileSystem Function in PHP: PART 15
  16. FileSystem Function in PHP: PART 16
PHP rewind() function

The PHP FileSystem rewind function rewinds the position of a file pointer and it returns true on success or false on failure.

Syntax

 
rewind(file)

Parameter in rewind function

The parameter of the function is:
 
Parameter Description
file It specifies the open file.

Example

An example of the
function is:

<?php

$filename=fopen("test.txt","r");

echo "file pointer position ".ftell($filename);

fgets($filename);

echo "<br>file pointer position ".ftell($filename);

rewind($filename);

echo "<br>after rewind file pointer position ".ftell($filename);

?>

Output

rewind-function-in-php.gif

PHP rmdir() function

The PHP FileSystem rmdir function removes a directory and it returns true on success or false on failure.

Syntax

rmdir(dir,context)

Parameter in rmdir function

The parameters of the function are:

Parameter Description
dir It specifies the directory to be removed.
context It  specifies the context of the file handle.

Example

An example of the
function is:

<?
php
$
folderpath="sharad";
if
(!rmdir($folderpath))

{

echo ("could not remove $folderpath");

}

else

{

echo "remove successfully.";

}

?>

Output

rmdir-function-in-php.gif

PHP set_file_buffer() function

The PHP FileSystem set_file_buffer function is the alias of stream_set_write_buffer function and is also used to set the buffer size of an open file.

Syntax

set_file_buffer(file,buffer)

Parameter in set_file_buffer function

The parameters of the function are:

Parameter Description
file It specifies the open file.
context It  specifies the buffer size in bytes.

Example

An example of the
function is:

<?
php
$
file=fopen("text.txt","w");
$handle=set_file_buffer($file,100);
if($handle)
{
echo "
buffer is set";
}
else
{
echo "
buffer is not set";
}

?>

Output

set-file-buffer-in-php.gif

PHP stat() function

The PHP FileSystem stat function provides the information about a file and returns an array with the following formats:

Numeric Associative Description
0 dev It specifies device number.
1 ino It specifies inode number.
2 mode It specifies inode protection mode.
3 nlink It specifies number of links.
4 uid It specifies user id of owners
5 gid It specifies group id of owners.
6 rdev It specifies inode device type.
7 size It specifies size in butes.
8 atime It specifies the last time of access.
9 mtime It specifies the last modification time.
10 ctime It specifies the last inode change time.
11 blksize It specifies the block size of the file system IO.
12 blocks It specifies the number of blocks allocated.

Syntax

stat(filename)

Parameter in stat function

The parameter of the function is:

Parameter Description
filename It specifies the path to the file.

Example

An example of the
function is:

<?
php
$
stat = stat('test.txt');
echo '
Acces time: ' .$stat['atime']."</br>";
echo '
Number of links: ' .$stat['nlink']."</br>";
echo '
Modification time: ' .$stat['mtime']."<br>";
echo '
Device number: ' .$stat['dev']."</br>";
echo '
Size in Bytes: ' .$stat['size'];
?>


Output


stat-function-in-php.gif

PHP symlink() function

The PHP FileSystem symlink function creates a symbolic link and it returns true on success and false on failure.

Syntax

symlink(target,link)

Parameter in symlink function

The parameters of the function are:

Parameter Description
target It specifies the target of the link.
link It specifies the link name.

Example

An example of the
function is:

<?
php
$
target = 'test.php';
$
link = 'abc';
symlink
($target, $link);
?>

Up Next
    Ebook Download
    View all
    Learn
    View all