3/31/15

PHP File Handling


The file system functions allow you to access and manipulate the file.
file system provides a concept to start a specific data using different types of file format.
That means file gives us linear type database concept. There is no any type of relations will be found with value because it doesn’t support RDBMS concept.
Through this concept you will retrieved data from disk filesXML documents and many other data sources.
Files In a computer, a file system (sometimes written filesystem) is the way in which files are named and where they are placed logically for storage and retrieval.
The DOS, Windows, OS/2, Macintosh, and UNIX-based operating systems all have file systems in which files are placed somewhere in a hierarchical (tree) structure.
A file is placed in a directory (folder in Windows) or sub-directory at the desired place in the tree structure.

How to Create a file using PHP

touch( ) function is used to create a file
Syntax
 <?php
    touch("fileName with extension");
 ?>

Eg i
 <?php 
  //create a ms word file
  touch("resume.doc");
  
  //create text file
  touch("data.txt");
  
  //create pdf file
  touch('corephp.pdf');
 ?>
 
  Output : Check your folder manually
(same folder where you have saved your program) a file will be created

How to Delete a file using PHP

unlink( ) function is used to delete a file
Syntax
  <?php
   unlink("fileName with extension");
  ?>
Eg ii
 <?php 
  //delete  resume word file
  unlink("resume.doc");
  
  //delete text file
  unlink("data.txt");
  
  //delete pdf file
  unlink('corephp.pdf');
 ?>
 
  Output : Check your folder manually
(same folder where you have saved your program) a file will be deleted

How to copy a file using PHP
copy( ) function is used to copy file
Syntax
 <?php
 copy("source file with extension","destination fileName with same extension");
 ?>
Eg iii
 <?php 
  //copy resume doc
  copy("resume.doc","Update resume.doc");
  
  //copy text file
  copy("data.txt","update data.txt");    
 ?>
 
  Output : Check your folder manually
(same folder where you have saved your program) a file will be copied with new name

How to Rename file using PHP
rename( ) function is used to rename file.
Syntax
<?php
   rename("old fileName with extension","New fileName with same extension");
?>
Eg iv
 <?php 
  //rename resume doc
  rename("resume.doc","Update resume.doc");
  
  //rename text file
  rename("data.txt","update data.txt");    
 ?>
 
  Output : Check your folder manually
(same folder where you have saved your program) a file will be renamed
Checks whether a file or directory exists
file_exists( ) function is used to check file or directory existence.
Syntax
<?php
   file_exists("fileName with extension");
    OR
  file_exists("directory name");
?>
Eg v
 <?php 
  //check file existence
  echo file_exists("Update resume.doc");
      
 ?>
 
  Output : It returns true(1) if file exists
        otherwise return false(blank screen)

Check size of the file in PHP
filesize( ) function is used to check file size.
Syntax
<?php
   filesize("fileName with extension");
?>
Eg vi
 <?php 
  //check file size
  echo filesize("Update resume.doc")." Bytes";   
 ?>
 
  Output : 0 Bytes

Check Path of the file in PHP
realpath( ) function is used to check real path of the file.
Syntax
<?php
   realpath("fileName with extension");
?>
Eg v
 <?php 
  //check real path of the file
  echo realpath("Update resume.doc");
      
 ?>
 
  Output : C:\xampplite\htdocs\YoufProject\fileName with extension
                 OR
        C:\wamp\www\YoufProject\fileName with extension    

0 comments:

Post a Comment

FIND US ON FACEBOOK

FIND US ON Twitter