PHP File System Functions

By | September 2, 2020
PHP File System Functions

PHP supports some functions that enable us to access and manipulate with the file system. These functions require some runtime configuration settings that can be done in the php.ini file. Below is the list of settings that you can make in the php.ini file.

Name Default Changeable Changelog
allow_url_fopen “1” PHP_INI_ALL PHP_INI_ALL in PHP <= 4.3.4. PHP_INI_SYSTEM in PHP < 6. Available since PHP 4.0.4.
allow_url_include “0” PHP_INI_ALL PHP_INI_SYSTEM in PHP 5. Available since PHP 5.2.0.
user_agent NULL PHP_INI_ALL Available since PHP 4.0.3.
default_socket_timeout “60” PHP_INI_ALL Available since PHP 4.3.0.
from “” PHP_INI_ALL
auto_detect_line_endings “0” PHP_INI_ALL Available since PHP 4.3.0.

PHP Constants

GLOB_BRACE, GLOB_ONLYDIR, GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION, PATHINFO_FILENAME, FILE_USE_INCLUDE_PATH, FILE_APPEND, FILE_IGNORE_NEW_FILES, FILE_SKIP_EMPTY_LINES, FILE_BINARY, FILE_TEXT.

Vamware

PHP Filesystem Functions

basename()

This function will provide you with the basename of the file. This function will take two parameters, the first path parameter is mandatory which specifies the file path and the second parameter is the suffix which specifies the file suffix.

Syntax

string basename ( string $path [, string $suffix] );

Example

<?php
   $path_demo = "/home/httpd/xxx/index.php";
   $file = basename($path_demo);
   echo "File name is $file\n";
$file = basename($path_demo, ".php");
   echo "File name is $file\n";
?>

chgrp()

This function will allow you to change the group of the provided filename to group. This function will take two parameters the filename and the group name

Syntax

bool chgrp ( string $filename, mixed $group );

Example

<?php
   $filename = "/home/httpd/xxx/index.php";
   chgrp ( $filename, "guest" );
?>

chmod()

This function will allow you to change the mode of the provided filename. This function will take two parameters filename and the mode that you want to apply to that filename. Both the passed parameter is mandatory.

Syntax

bool chmod ( string $filename, int $mode );

Example

<?php
   chmod("hello.txt", 0600);
?>

chown()

This function will allow you to change the owner of the provided filename with the specified owner name. The user who has the superuser properties can only change the owner of the provided filename.

Syntax

bool chown ( string $filename, mixed $user );

Example

<?php
   $path = "/home/httpd/xxx/index.php";
   $user_name = "root";
   chown($path, $user_name);         
?>

clearstatcache()

These functions will enable caching the information in order to provide faster performance. But if you want to clear up the cache then this function will solve the purpose. This function will erase the cache that is stored about any file on the PHP. there is no need to pass any parameter for this function.

Syntax

 void clearstatcache ( void );

Example

<?php
   clearstatcache();         
?>

copy()

This function will allow you to create a copy of the source file to the provided destination. This function will take two mandatory parameters- the source string and the destination string to specify the file location or path.

Syntax

bool copy ( string $source, string $dest );

Example

<?php
   $file = '/usr/home/xxx/example.txt';
   $newfile = '/usr/home/xxx/example.txt.bak';
  echo "success\n";
   }
?>

dirname()

This function will provide you with the directory name list that is available at the provided path. This function will take a single parameter that is the path or location.

Syntax

 string dirname ( string $path );

Example

<?php
   $path_demo = "/home/httpd/xxx/index.php";
   $dir_demo = dirname($path_demo);
   echo "dirname is $dir_demo\n";
?>

disk_free_space()

This function will provide you with the details about how many bytes are available on the specified filesystem or the disk partition. This function will take up the single mandatory parameter that is the directory name.

Syntax

float disk_free_space ( string $directory );

Example

<?php
   disk_free_space("C:");
?>

diskfreespace()

This function can also be used as disk_free_space() function. This function also take a single parameter which is the directory name for which we want to get the information about the free space available.

Syntax

float diskfreespace ( string $directory );

Example

<?php
   $hdwinC = diskfreespace("C:");
   echo "Diskspace left - $hdwinC";
?>

fclose()

This function will close the file which was opened by using fopen() or fsockopen() function. This function will take the pointer for the specific file which we want to close.

Syntax

bool fclose ( resource $handle );

Example

<?php
   $handle = fopen('sampel.txt', 'r');
   fclose($handle);
?>

feof()

This function will allow you to check the pointer for the end-of-file. In case, if you provide the file pointer which is not valid then it will be stuck into an infinite loop because the function fails to return true. This function will take the file pointer as a parameter which should be valid and not yet closed.

Syntax

bool feof ( resource $handle );

Example

<?php
   if ($file_demo = fopen('C;/index.txt', 'rw')) do {
      $line = fgets($file_demo);
      echo "$line";
   }
   while (!feof($file_demo));
   fclose($file_demo);
?>

fflush()

This function enables you to force the write for all buffered output to the resource that is being pointed to by the specified file handle. This function will take-up a single mandatory parameter which is a file pointer and needs to be valid.

Syntax

bool fflush ( resource $handle );

Example

<?php
   if ($f = fopen('sample.txt', 'rw')) do {
      $l = fgets($f);
      echo "$l";     
      // do any stuff here...
   }
   while (!feof($f));
   fflush($f);
   // do any stuff here...
   fclose($f);
?>

fgetc()

This function will allow you to get the character from the specified file pointer. This function will take a single mandatory parameter this is the file pointer which should be valid.

Syntax

string fgetc ( resource $handle );

Example

<?php
   $file_open = fopen(index.txt', 'rw');
   if (!$file_open) {
      echo 'file cannot be open';
   }
   while (false !== ($char = fgetc($file_open))) {
      echo "$char\n";
   }
?>

fgetcsv()

This function will work similarly to fgets() but it parses the line it reads for CSV fields and will return an array that will contain all the information.

Syntax

array fgetcsv ( resource $handle [, int $length [, string $delimiter  [, string $enclosure [, string $escape]]]] );

Where handle is a valid file pointer, the length should be greater than the longest file, delimiter specifies the field delimiter, enclosure specifies the field enclosure character and the last parameter sets the escape character.

Example-

<?php
   row = 1;
   $handle = fopen("index.csv", "w");
   while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
      $num = count($data);
      $row++;
      for ($c=0; $c < $num; $c++) {
         echo $data[$c] . "<br />\n";
      }
   }
fclose($handle); ?>

fgets()

This function will provide you with the line from the specified file pointer. This function will take two parameters where the first parameter is the file pointer and the length specifies the length (length-1) where the reading will end if there is no length parameter is specified then it will read until it reaches the end of the file.

Syntax

 string fgets ( resource $handle [, int $length] );

Example

<?php
   $handle_demo = @fopen("/xxx/index.txt", "w");
   if ($handle_demo) {
      while (!feof($handle_demo)) {
         $buffer = fgets($handle_demo, 512);
         echo $buffer;
      }
      fclose($handle_demo);
   }
?>

fgetss()

This function will work similarly to fgets() function except for the part that this function will strip HTML and PHP tags from the reading text. This function will accept three parameters where the first parameter is the file pointer, second is the length which specifies the length of the retrieved data and the last parameter is the tags that you do not want to strip.

Syntax

string fgetss ( resource $handle [, int $length [, string $allowable_tags]] );

Example

<?php
   $handle_demo = @fopen("/xxx/index.php", "r");
   if ($handle_demo) {
      while (!feof($handle_demo)) {
         $buffer = fgetss($handle_demo, 1024);
         echo $buffer;
      }
      fclose($handle_demo);
   }
?>

file_exists()

This function will check if the provided file or directory exists or not. This function will take a single mandatory parameter which specifies the path to a file or directory.

Syntax

bool file_exists ( string $file_open );

Example

<?php
   $file_open = '/home/xxx/sample.htm';
   if (file_exists($file_open)) {
      echo "$file_open exists";
   }else {
      echo " $file_open does not exist";
   }
?>

file_get_contents()

This function works similarly to file() function except for this function will return the filename in a string which starts at the provided offset and will be up to the maxlen bytes.

Syntax

string file_get_contents ( string $filename [, int $flags [, resource $context  [, int $offset [, int $maxlen]]]] );

Example

<?php
   $lines = file_get_contents('https://www.tutorialspoint.com/');
   echo $lines;
?>

file_put_contents()

This function will work like the combination of three functions- fopen(), fwrite() and fclose() in succession which writes the data to the file. If the provided filename does not exist then it will create a file else it will overwrite the existing file.

Syntax

int file_put_contents ( string $filename, mixed $data [, int $flags [, resource $context]] );

Example

<?php

   $input_path = "/home/xxx/sample.txt";
   $output_path = "/home/xxx/output.txt";
   $fi = fopen($input_path, 'w');
   $source = '';
   while (!feof($fi)) {
      $source .= fgets($fi);
   }
   fclose($fi);
   file_put_contents($output_path,$source_path);
?>

file()

This function will allow you to read the entire provided file into an array. This function will take three parameters- the filename, the flag and the context of the file created with stream_context_create() function.

Below are the flags used-

  • FILE_USE_INCLUDE_PATH − this will search for the file used in the include_path.
  • FILE_IGNORE_NEW_LINES − it will not add a newline each array element end.
  • FILE_SKIP_EMPTY_LINES − this flag will allow the function to skip empty lines.
  • FILE_TEXT − this flag will provide the content in UTF-8 encoding.
  • FILE_BINARY − this flag will allow the content to be read as the binary data.

Syntax

array file ( string $filename [, int $flags [, resource $context]] );

Example

<?php
   $lines = file('http://www.xxx.com/');
   foreach ($lines as $line_num => $line) {
      echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line)
      echo "<br />\n";
   }
?>

fileatime()

This function will allow you to provide the last access time of the file. This function will take a single mandatory parameter which specifies the path of the file.

Syntax

 int fileatime ( string $filename );

Example

<?php
   $file_name = "/home/xxx/html/index.php";
   if (file_exists($file_name)) {
      echo "file was last accessed: " . date("F d Y H:i:s.", fileatime($file_name));
   }
?>

filectime()

This function will allow you to get the change in inode time for a provided file. This function will take a single mandatory parameter which specifies the path of the file.

Syntax

int filectime ( string $filename )

Example

<?php
   $filen_ame = "/home/xxx/html/index.php";
   if (file_exists($file_name)) {
      echo "file was last changed: " . date("F d Y H:i:s.", filectime($file_name));
   }
?>

filegroup()

This function will allow you to get the group of the file. This will return the numeric group ID and use posix_getgrgid() in order to resolve it to a group name. This function will take a single mandatory parameter which specifies the file path.

Syntax

 int filegroup ( string $filename );

Example

<?php
   $file_name = "/home/xxx/html/index.php";
   $group = filegroup($file_name);
   echo "File group- $group\n";
?>

filenode()

he inode of the particular file is to be returned by this function. The inode number of the file will be returned on success.

Syntax

int fileinode ( string $filename )

Example

<?php
   $f_name = "/php_project/demo.txt";
   echo fileinode($f_name);
?>

Output

12666373952223775

filemtime()

This function will return the time when the file was last modified in the UNIX timestamp on the successful execution.

Syntax

int filemtime ( string $filename )

Example

<?php
   echo filemtime("/Php_Project/text.txt"); 
   echo "\n";
   echo "Last modified: ".date("F d Y H:i:s.",filemtime("/Php_Project/text.txt"));
?>

Output

1590392449
Last modified: May 25 2020 09:40:49.

fileowner()

This function will provide you with the owner or user id of the specific file only on success.

Syntax

int fileowner ( string $filename )

Example

<?php
   echo fileowner("/Php_Project/text.txt"); 
?>

Output

0

fileperms()

This function will provide you with the permissions any user has on the particular file.

Syntax

int fileperms ( string $filename )

Example

<?php
    echo substr(sprintf("%o",  fileperms("/Php_Project/text.txt")),-4); 
?>

Output

0666

filesize()

This function will provide you with the size of the specified file on success.

Syntax

int filesize ( string $filename )

Example

<?php
$f_name=("/Php_Project/text.txt"); 
   echo $f_name . ': ' . filesize($f_name) . ' bytes';
?>

Output

/Php_Project/text.txt: 27 bytes

filetype()

This function will provide you with the type of the specified file or the given directory.

Syntax

string filetype ( string filename )

Example

<?php
   echo filetype("/Php_Project/text.txt"); 
   echo "\n";
   echo filetype("/Php_Project/");
?>

Output

File
Dir

fnmatch()

This function will allow you to match the filename with the provided pattern.

Syntax

bool fnmatch ( string $pattern , string $string [, int $flags = 0 ] )

fopen()

This function will allow you to open a file or the URL. On failure this function will return either false or the error. You can provide @ at the start of the function to hide the error regarding that function.

Syntax

resource fopen ( string $filename , string $mode [, bool $use_include_path = FALSE [, resource $context ]] )

Example

<?php
   $file = fopen("/Php_Project/text.txt", "r");
?>

fpassthru()

This function will allow you to read the content from the current position within an open file till you reach the end of file and the result will be provided on the output buffer.

Syntax

int fpassthru ( resource $handle )

Example

<?php
   $f_name = fopen("/Php_Project/text.txt", "r");
   fgets($f_name);
   echo fpassthru($f_name);
   fclose($f_name);
?>

Output

Tex4

fputcsv()

This function will allow you to format the line in the csv format and then write it to the open file.

Syntax

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\\" ]]] )

Example

<?php
   $list = array (
      array('"aaa"', '"bbb"')   
   );
   $f_open = fopen("/Php_Project/text.csv", "w");
   foreach($list as $fields) {
      fputcsv($f_open, $fields);
   }
   echo "data has been inserted to .csv file";
   fclose($f_open);
?>

Output

data has been inserted to .csv file

fputs()

this function will allow you to write to an open file. This function will either write till the end of file or to the length that is specified. This function will also return the number of bytes if the execution is successful.

Syntax

fputs(file,string,length)

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
echo fputs($f_open, "Hello!!!!");
   fclose($f_open);
   fclose($f_open);
?>

Output

21

fread()- this function will allow you to read from the open file. This function will either write till the end of file or to the length that is specified. This function can also provide you with the read string on success.

Syntax

string fread ( resource $handle , int $length )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
   $var = fopen($f_open, "r");
   echo fread($var, "30");
   fclose($f_open);
?>

Output

Hello

fseek()

This function will allow you to seek in the open file. This function will allow the pointer to move in any direction that is specified by the byte number.

Syntax

int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
echo fgets($f_open);
   fseek($f_open, 0);
   fclose($f_open);
?>

Output

Hello
Hello

fstat()

this function will provide you with the information about the open file.

Syntax

array fstat ( resource $handle )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
 print_r(fstat($f_open));
   fclose($f_open);
?>

Output

Array
(
    [0] => 1245376677
    [1] => 12666373952223775
    [2] => 33206
    [3] => 1
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 49
    [8] => 1590217956
    [9] => 1590994836
    [10] => 1590217956
    [11] => -1
    [12] => -1
    [dev] => 1245376677
    [ino] => 12666373952223775
    [mode] => 33206
    [nlink] => 1
    [uid] => 0
    [gid] => 0
    [rdev] => 0
    [size] => 49
    [atime] => 1590217956
    [mtime] => 1590994836
    [ctime] => 1590217956
    [blksize] => -1
    [blocks] => -1
)

ftell()

The current position in the open file will be provided by this function.

Syntax

int ftell ( resource $handle )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
echo ftell($f_open);
   fseek($f_open, "10");
   echo "\n" . ftell($f_open);
   fclose($f_open);
?>

Output

1
15

ftruncate()

This function will allow you to truncate an open file to the length that is specified within the function.

Syntax

bool ftruncate ( resource $handle , int $size )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "+a"); 
   ftruncate($f_open, 100);
   fclose($f_open);
   clearstatcache();
   fclose($f_open);
?>

Output

50
23

fwrite()

This function will allow you to write to an open file. This function will either stop at the end of the file or at the length specified within the function. This function will provide the number of bytes written to the file.

Syntax

int fwrite ( resource $handle , string $string [, int $length ] )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
 echo fwrite($f_open, "Hello!!!!!");
   fclose($f_open);
?>

Output

5

glob()

This function will provide you with the array that contains the file name and the directories that matches the specified patten given within the function

Syntax

array glob ( string $pattern [, int $flags = 0 ] )

Example

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

Output

Array
(
    [0] => /Php_Project/php/test.txt
}

is_dir()

This function will allow you to check if the given file is a directory or not. The true value will be returned if the directory exists.

Syntax

bool is_dir ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
if(is_dir($f_open)) {
      echo ("$f_open is a directory");
   } else {
      echo ("$f_open is not a directory");
   }
//fclose($f_open);
?>

Output

/Php_Project/text.csv is not a directory

is_executable()

This function will allow you to check if the given file is executable or not. True value will be returned if the file is executable.

Syntax

bool is_executable ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/sam.exe");
if(is_executable($f_open)) {
      echo ("$f_open is a executable");
   } else {
      echo ("$f_open is not a executable");
   }
   //fclose($f_open);
?>

Output

/Php_Project/sam.exe is a executable

is_file()

This function will allow you to check if the specified file a regular file or not.

Syntax

bool is_file ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/text.csv", "w");
if(is_file($f_open)) {
      echo ("$f_open is a regular file");
   } else {
      echo ("$f_open is not a regular file");
   }
   //fclose($f_open);
?>

Output

/Php_Project/text.csv is a regular file

is_link()

This function will allow you to check if the given file is a link or not.

Syntax

bool is_link ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/images");
if(is_link($f_open)) {
      echo ("$f_open is a link");
   } else {
      echo ("$f_open is not a link");
   }
   //fclose($f_open);
?>

Output

/Php_Project/images is a link

is_readable()

This function will allow you to check if the given file is readable or not.

Syntax

bool is_readable ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/text.txt");
if(is_readable($f_open)) {
      echo ("$f_open is a readable");
   } else {
      echo ("$f_open is not a readable");
   }
   //fclose($f_open);
?>

Output

/Php_Project/text.txt is a readable

is_uploaded_file()

This function will allow you to check if the file gets uploaded by HTTP POST.

Syntax

bool is_uploaded_file ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/text.txt");
if(is_uploaded_file($f_open)) {
      echo ("$f_open is uploaded via HTTP POST");
   } else {
       echo ("$f_open is not uploaded via HTTP POST");
   }
   //fclose($f_open);
?>

Output

/Php_Project/text.txt is uploaded via HTTP POST

is_writable()

This function will allow you to check if the given file is writable or not.

Syntax

bool is_writable ( string $filename )

Example

<?php
   $f_open = fopen("/Php_Project/text.txt");
if(is_writable($f_open)) {
      echo ("$f_open is a writable");
   } else {
      echo ("$f_open is not a writable");
   }
   //fclose($f_open);
?>

Output

Php_Project/text.txt is a writable

lchgrp()

This function will allow you to change the ownership as a group of the symlink. Only the superuser can change this property.

Syntax

bool lchgrp ( string $filename , mixed $group )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
 $file  = "/PhpProject/test.html";
   symlink($target, $file);
   lchgrp($file, 8);
   //fclose($f_open);
?>

lchown()

This function will allow you to change the owner of the symlink. This property can also be changed by the superuser.

Syntax

bool lchown ( string $filename , mixed $user )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
 $file  = "/PhpProject/test.html";
 $target = "/PhpProject/samp.php";
   symlink($target, $file);
   lchown($file, 8);
   //fclose($f_open);
?>

link()

This function will allow you to create a hard link using the existing target with the provided link in the function.

Syntax

bool link ( string $target , string $link )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
 $file  = "/PhpProject/test.html";
 $target = "/PhpProject/samp.php";
  link($target, $file);
   echo "linked files";
   //fclose($f_open);
?>

Output

linked files

linkinfo()

This function will provide you with the hard link information and the device ID.

Syntax

int linkinfo ( string $path )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
   echo linkinfo("/Php_Project/text.txt"); 
   //fclose($f_open);
?>

lstat()

This function will provide you with the information about the file or the symbolic link.

Syntax

array lstat ( string $filename )

Example

<?php
   print_r(lstat("/Php_Project/text.txt"));
   //fclose($f_open);
?>

mkdir()

This function will allow you create a directory and the function return true if the execution is successful

Syntax

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = FALSE [, resource $context ]]] )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
   mkdir("/Php_Project/test");
   echo "Directory created";
   //fclose($f_open);
?>

Output

Directory created

move_uploaded_file()

This function will allow you to move a file to the new location specified within the function. If there is no valid file then the function will return false.

Syntax

bool move_uploaded_file ( string $filename , string $destination )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
   $dir=("/Php_Project/test");
  foreach($_FILES["pictures"]["error"] as $key => $error) {
      if($error == UPLOAD_ERR_OK) {
         $f_name = $_FILES["pictures"]["tmp_name"][$key];
         $name = basename($_FILES["pictures"]["name"][$key]);
         move_uploaded_file($f_name, "$dir/$name");
      }
   }
   //fclose($f_open);
?>

parse_ini_file()

This function will allow you to parse a config file and the details are provided within an array.

Syntax

array parse_ini_file ( string $filename [, bool $process_sections = FALSE [, int $scanner_mode = INI_SCANNER_NORMAL ]] )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
  print_r(parse_ini_file("/Php_Project/simple.ini"));
   //fclose($f_open);
?>

Output

Array
(
    [me] => Aashiya
    [you] => Sam
    [first] => http://www.XXX.com
    [second] => https://www.google.com
)

pathinfo()

This function will provide you with an array that will have the details about the path of the file. If you will not pass the option parameter then it will provide an associative array.

Syntax

mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

Example

<?php
   //$f_open = fopen("/Php_Project/text.txt");
   print_r(pathinfo("/Php_Project/simple.ini"));
   //fclose($f_open);
?>

Output

Array
(
    [dirname] => /Php_Project
    [basename] => text.txt
    [extension] => txt
    [filename] => simple
)

pclose()

This function will allow you to close the pipe that is opened by using the popen function. The termination status of process will be returned.

Syntax

int pclose ( resource $handle )

Example

<?php
 $f_name = popen("/bin/ls", "r");
 pclose($f_name);
?>

popen()

This function will allow you to open program pipe that is specified by the command parameter in the function.

Syntax

resource popen ( string $command , string $mode )

Example

<?php
   $file = popen("/bin/ls", "r");
?>

readfile()

This function will allow you to read the file and then write it to the buffer output. The number of bytes will be provided if the function runs successfully.

Syntax

int readfile ( string $filename [, bool $use_include_path = FALSE [, resource $context ]] )

Example

<?php
  echo readfile("/Php_Project/text.txt");
?>

Output

Hello

readlink()

This function will provide you with the symbolic link targe. The target of the link will be provided on success.

Syntax

string readlink ( string $path )

Example

<?php
  echo readlink("/Php_Project/samlink");
?>

realpath()

This function will provide you with the absolute pathname for the provided file.

Syntax

string realpath ( string $path )

Example

<?php
   echo $path = realpath("/Php_Project/samlink");
?>

Output

C:\Php_Project\samlink

rename()

This function will allow you to rename the existing file name with the newly provided file name. If the new name you provide already exists then it will override the file.

Syntax

bool rename ( string $oldname , string $newname [, resource $context ] )

Example

<?php
   rename("/Php_Project/text.txt", "/Php_Project/php/text1.txt");
   echo "file renamed";
   ?>

Output

file renamed

rewind()

This function will allow you to rewind the file pointer position to the file beginning and provide the true value on success.

Syntax

bool rewind ( resource $handle )

Example

<?php
   $file = fopen("/Php_Project/text.txt", "r+");
   fwrite($file, "Hello");
   rewind($file);
   fwrite($file, "Bye");
   rewind($file);
   echo fread($file, filesize("/Php_Project/text.txt"));
   fclose($file);
   ?>

Output

Hello Bye

rmdir()

This function will allow you to remove the directory that is empty and have required permissions to do so. If not then error will be generated.

Syntax

bool rmdir ( string $dirname [, resource $context ] )

Example

<?php
      if(!is_dir("/Php_Project/sam.txt")) {
      mkdir("/Php_Project/sam.txt");
   }
   rmdir("/Php_Project/sam.txt");
   echo "Directory removed!";
   ?>

Output

Directory removed

symlink()

This function will allow you to create a symbolic link using the existing target with the provided name link in the function.

Syntax

bool symlink ( string $target , string $link )

Example

<?php
   $target = "/Php_Project/test.txt";
   $test_link = "/test";
   symlink($target, $test_link);
   echo readlink($test_link);
   ?>

tempnam()

This function will allow you to create a temporary file within the specified directory. You can also create a new temp file with the provided path.

Syntax

string tempnam ( string $dir, string $prefix )

Example

<?php
  echo tempnam("C:\Php_Project", "TMP0");
   ?>

tmpfile()

This function will allow you to create a temporary file with the read-write permission.

Syntax

resource tmpfile ( void )

Example

<?php
   $tm_file = tmpfile();
   fwrite($tm_file, "welcome");
   rewind($tm_file);  
   echo fread($tm_file, 1024); 
   fclose($tm_file);  
   ?>

Output

welcome

touch()

With this function you will be able to set the access and the modification time for the specified file.

Syntax

bool touch ( string $filename [, int $time = time() [, int $atime ]] )

Example

<?php
   $f_name = "/Php_Project/text.txt";
   if(touch($f_name)) {
      echo $f_name . " modification done";
   } else {
      echo "could not change modification time" . $f_name;
   }
   ?>

output

/Php_Project/text.tx modification done

umask()

Using this function you will be able to change the file permission. The PHP umask to mask and 0777 are set and the old mask will be returned. If there is no parameters passed then you will get the current mask.

Syntax

 int umask ([ int $mask ] )

Example

<?php
   $old_mask = umask(0);
   chmod("Php_Project/php/text.txt", 0755);
   umask($old_mask);
   if($old_mask != umask()) {
      echo "error occurred";
   }
   ?>

Output

error occurred

unlink()

This function will allow you to delete the specified file.

Syntax

bool unlink ( string $filename [, resource $context ] )

Example

<?php
   $f_name = "/Php_Project/text.txt";
   if(!unlink($f_name)) {
      echo ("Error deleting $f_name");
   } else {
      echo ("Deleted $f_name successfully");
   }
   ?>

Output

Deleted /Php_Project/text.txt successfully

Leave a Reply

Your email address will not be published. Required fields are marked *