File Handling
What is File?
 A collection of data or information.
 One that has a name, called the filename.
 Almost all information ...
Tracks , Sectors And Clusters
 Tracks
 The tracks are the thin concentric circular strips of sectors.
At least one head ...
Files In Php
 Php provides some functions for handling files.
 We can create, write , read, append, copy and their
permi...
fopen
 The PHP fopen() function is used to open a file. It requires two arguments
stating first the file name and then mo...
fwrite()
 A new file can be written or text can be appended to an
existing file using the PHP fwrite() function.
 This f...
Reading A File
 Once a file is opened using fopen() function it can be
read with a function called fread().
 This functi...
Magic Constants
 __File__
 __Line__
 __DIR__
 is_dir() — Tells whether the filename is a directory.
 is_file() — Tell...
Questions
?
Questions
?
Upcoming SlideShare
Loading in...5
×

16-Filing lecture 1 php

511

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
511
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

16-Filing lecture 1 php

  1. 1. File Handling
  2. 2. What is File?  A collection of data or information.  One that has a name, called the filename.  Almost all information stored in a computer must be in a file.  There are many different types of files:  data files  text files  program files  directory files and so on.  Different types of files store different types of information. For example, program files store programs, whereas text files store text.
  3. 3. Tracks , Sectors And Clusters  Tracks  The tracks are the thin concentric circular strips of sectors. At least one head is required to read a single track.  Sectors  A sector is the smallest storage unit that is addressable by a hard drive, and all information stored by the hard drive is recorded in sectors.  Blocks and clusters  The term block to refer to a sector or group of sectors.
  4. 4. Files In Php  Php provides some functions for handling files.  We can create, write , read, append, copy and their permission can be set/done.  fopen — Opens file or URL  fwrite — Binary-safe file write  fread — Binary-safe file read  filesize — Gets file size  fgets — Gets line from file pointer  feof — Returns true at end of file
  5. 5. fopen  The PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then mode in which to operate.  Files modes can be specified as one of the six options in this table. Mode Purpose r • Opens the file for reading only. • Places the file pointer at the beginning of the file. r+ • Opens the file for reading and writing. • Places the file pointer at the beginning of the file. w • Opens the file for writing only. • Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file. w+ • Opens the file for reading and writing only. • Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attemts to create a file. a • Opens the file for writing only. • Places the file pointer at the end of the file. • If files does not exist then it attemts to create a file. a+ • Opens the file for reading and writing only. • Places the file pointer at the end of the file. • If files does not exist then it attemts to create a file. x • Create and open for writing only; place the file pointer at the beginning of the file. • If the file already exists, the fopen() call will fail by returning FALSE If the file does not exist, attempt to create it. X+ • Create and open for reading & writing only; place the file pointer at the beginning of the file. • If the file already exists, the fopen() call will fail by returning FALSE If the file does not exist, attempt to create it.
  6. 6. fwrite()  A new file can be written or text can be appended to an existing file using the PHP fwrite() function.  This function requires two arguments  specifying a file pointer  The string of data that is to be written. <?php $filename = “test.txt"; $file = fopen( $filename, "w" ); if( $file == false ) { echo ( "Error in opening new file" ); exit(); } fwrite( $file, "This is a simple testn" ); fclose( $file ); ?>
  7. 7. Reading A File  Once a file is opened using fopen() function it can be read with a function called fread().  This function requires two arguments:  The file pointer  The length of the file expressed in bytes.  The file's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.  So here are the steps required to read a file with PHP.  Open a file using fopen() function.  Get the file's length using filesize() function.  Read the file's content using fread() function.  Close the file with fclose() function.
  8. 8. Magic Constants  __File__  __Line__  __DIR__  is_dir() — Tells whether the filename is a directory.  is_file() — Tells whether the filename is a regular file.  file_exists ()— Checks whether a file or directory exists  file_get_contents(filename);  file_put_contents(filename);
  9. 9. Questions ?
  10. 10. Questions ?

×