SlideShare a Scribd company logo
Filing System
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.
OR
 A file is an object on a computer that stores data, information, settings, or commands that are
used with a computer program. In a graphical user interface (GUI) such as Microsoft Windows,
files are shown as unique icons that relate to the program that opens the file.
FILE SYSTEM : magic constants
• MAGIC CONSTANTS
• __FILE__ // The full path and filename of the file
• __LINE__ // The current line number of the file.
• __DIR__ // The directory of the file. (only php 5.3)
• PHP FUNCTION
• dirname(__FILE__);
• File_exists(__FILE__);
• File_exists(dirname(__FILE__) )
• File_exists(dirname(__FILE__).”/basic.html”) ? “yes”:”No”
• Is_file() ;
• Is_file(dirname(__FILE__).”/basic.html” ) ? “y”:”n”
• Is_dir();
• Is_file(dirname(__FILE__) ) ? “y”:”n”
UNDERSTANDING FILE PERMISSIONS: windows file
permissions
PHP AND FILE PERMISSIONS
• Octal notation: 764
PHP AND FILE PERMISSIONS
• Chown()
• Chown(‘file_name.php’,’owner_name’) ;
• Chown only works in php is super user
• Making webserver/PHP a super user is a big issue
• Chmod();
• Fileperms();
• Fileperms(‘file_name.php’) // i.e 103306
• Decoct();
• Decoct(fileperms(‘file_name.php’))
• Sprintf()
• Is_readable();
• Is_writable();
ACCESSING FILES
• 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 —
• fread —
• filesize — Gets file size
• fgets — Gets line from file pointer
ACCESSING FILES
• ACCESSING FILES
• fopen( filename , mode );
• Note: When writing to a text file, be sure to use the correct line-ending character! Unix
systems use n, Windows systems use rn, and Macintosh systems use r as the line
ending character.
ACCESSING FILES
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.
$file = ‘file-name.txt’;
$handle = fopen( $file , ‘w’)
fclose( $handle );
If ( $handle = fopen( $file , ‘w’)){
flose($handle );
}
ACCESSING FILES
$file = ‘file-name.txt’;
If($handle = fopen( $file , ‘w’) ) { overwrite
Fwrite( $file , “abc”);
Fwrite( $file , ‘123’) ;
Fwrite ( $file , ‘abcn123’) ;
}
WRITING TO FILES
• 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.
READING FILES
if( $handle = fopen( $file , 'r') ){
//$filesize = filesize( $file );
//$contents = fread( $handle ,$filesize );
//echo fgets( $handle);
while(!feof( $handle )){
$contents .= fgets( $handle );
}
}
echo $contents ;
echo nl2br($contents);
READING FILES
$file = ‘filetext.txt’;
If($handle = fopen( $file , ‘w’)){
Fwrite($handle , “123n456n789”);
$pos = ftell ($handle);
Fwrite( $handle , ‘abcdef’);
Rewind($handle );
Fwrite( $handle , ‘xyz’);
Fclose($handle);
// beware, it will overtype!!!
//Note : a and a+ modes will not let you move the pointer
}
MOVING THE FILE POINTER
• File_put_conents : shortcut for fopen / fwrite / fclose
$file = ‘filetest.txt’;
$content = “111n222n333”;
if( $size = file_put_contents( $file , $conent )){
Echo “a file of {$sie} bytes was created ”;
}
WRITING TO FILES
• Close files first
• Cant delete open files
• Must have permission on folder containing the file
Unlink(‘file_name’);
DELETING FILES
• getcwd — Gets the current working directory.
• rmdir — Removes directory.
• mkdir — Makes directory.
• chdir — Change directory.
• rename — Renames a file or directory.
• readdir — Read entry from directory handle.
• opendir — Open directory handle.
• pathinfo — Returns information about a file path.
WORKING WITH DIRECTORIES
• readfile — Reads the contents of a file and returns the number of
bytes read on success
Other PHP File System Functions

More Related Content

PPTX
Php File Operations
PPT
PHP - Introduction to File Handling with PHP
DOCX
Php files
PDF
Php file handling in Hindi
PPT
Files and Directories in PHP
PPT
Php File Operations
PPT
php file uploading
PDF
PHP file handling
Php File Operations
PHP - Introduction to File Handling with PHP
Php files
Php file handling in Hindi
Files and Directories in PHP
Php File Operations
php file uploading
PHP file handling

What's hot (19)

PPTX
File-Data structure
PPTX
R- create a table from a list of files.... before webmining
PPTX
Remove Duplicate Files & Manage
PDF
PDF
File system
PDF
PPT
PDF
TDC São Paulo 2016 - Become a jedi with php streams
PDF
scdevsumit 2016 - Become a jedi with php streams
PPTX
Data file operations in C++ Base
PPT
File in C Programming
PDF
TDC2016SP - Become a jedi with PHP streams
PPT
File handling in 'C'
PPT
File in c
PPT
File Management
PPT
Php i basic chapter 4
PDF
Module 03 File Handling in C
File-Data structure
R- create a table from a list of files.... before webmining
Remove Duplicate Files & Manage
File system
TDC São Paulo 2016 - Become a jedi with php streams
scdevsumit 2016 - Become a jedi with php streams
Data file operations in C++ Base
File in C Programming
TDC2016SP - Become a jedi with PHP streams
File handling in 'C'
File in c
File Management
Php i basic chapter 4
Module 03 File Handling in C
Ad

Viewers also liked (20)

PDF
City of Salina Booklet - After the Fire
PPTX
Multimodal Design for Technical Writing
PPT
Perfect papers software
KEY
Why Dropbox Rocks
PPT
Managing Files
PPTX
Files and directories in Linux 6
PPTX
Word Processing
PDF
Mail merge made easy
PPTX
Spreadsheets Concepts and Vocab
PPTX
Display devices and printers
PPTX
Windows File Systems
PPTX
Word Processing Introduction
PPTX
Parts of letter
PPT
The Power Of Mail Merge!
PPTX
Microsoft Excel 2016 Basics Course contents
PPTX
Word version 2016
PPTX
drive, one drive, dropbox
PPTX
What is word processing software
PPT
Word Processing Powerpoint
PDF
Maricel olleres how_to_use_dropbox
City of Salina Booklet - After the Fire
Multimodal Design for Technical Writing
Perfect papers software
Why Dropbox Rocks
Managing Files
Files and directories in Linux 6
Word Processing
Mail merge made easy
Spreadsheets Concepts and Vocab
Display devices and printers
Windows File Systems
Word Processing Introduction
Parts of letter
The Power Of Mail Merge!
Microsoft Excel 2016 Basics Course contents
Word version 2016
drive, one drive, dropbox
What is word processing software
Word Processing Powerpoint
Maricel olleres how_to_use_dropbox
Ad

Similar to Filing system in PHP (20)

PPTX
Ch3(working with file)
PPTX
Files in php
PDF
Web Development Course: PHP lecture 3
PPTX
PHP Filing
PPTX
File accessing modes in c
PPTX
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
PPSX
DIWE - File handling with PHP
PPTX
File handing in C
PPTX
PHP File Handling
PPTX
Working of File System: An Overview in detail
PPTX
Chap 5 php files part 1
PPTX
lecture 10.pptx
PPTX
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
PPTX
File handling
PPTX
WjkjkjkjkjiohuhuhiuhkhiuhkjjjjjjjjjjK-12-13-file.pptx
PPTX
Topic - File operation.pptx
PPTX
File Handling.pptx
Ch3(working with file)
Files in php
Web Development Course: PHP lecture 3
PHP Filing
File accessing modes in c
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
DIWE - File handling with PHP
File handing in C
PHP File Handling
Working of File System: An Overview in detail
Chap 5 php files part 1
lecture 10.pptx
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
File handling
WjkjkjkjkjiohuhuhiuhkhiuhkjjjjjjjjjjK-12-13-file.pptx
Topic - File operation.pptx
File Handling.pptx

More from Mudasir Syed (20)

PPT
Error reporting in php
PPT
Cookies in php lecture 2
PPT
Cookies in php lecture 1
PPTX
PPT
Reporting using FPDF
PPT
Oop in php lecture 2
PPT
Oop in php lecture 2
PPT
Time manipulation lecture 2
PPT
Time manipulation lecture 1
PPT
Php Mysql
PPT
Adminstrating Through PHPMyAdmin
PPT
Sql select
PPT
PHP mysql Sql
PPT
PHP mysql Mysql joins
PPTX
PHP mysql Introduction database
PPT
PHP mysql Installing my sql 5.1
PPT
PHP mysql Er diagram
PPT
PHP mysql Database normalizatin
PPT
PHP mysql Aggregate functions
PPT
Form validation with built in functions
Error reporting in php
Cookies in php lecture 2
Cookies in php lecture 1
Reporting using FPDF
Oop in php lecture 2
Oop in php lecture 2
Time manipulation lecture 2
Time manipulation lecture 1
Php Mysql
Adminstrating Through PHPMyAdmin
Sql select
PHP mysql Sql
PHP mysql Mysql joins
PHP mysql Introduction database
PHP mysql Installing my sql 5.1
PHP mysql Er diagram
PHP mysql Database normalizatin
PHP mysql Aggregate functions
Form validation with built in functions

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Business Ethics Teaching Materials for college
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
O7-L3 Supply Chain Operations - ICLT Program
Business Ethics Teaching Materials for college
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Week 4 Term 3 Study Techniques revisited.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Complications of Minimal Access Surgery at WLH
PPH.pptx obstetrics and gynecology in nursing
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.

Filing system in PHP

  • 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. OR  A file is an object on a computer that stores data, information, settings, or commands that are used with a computer program. In a graphical user interface (GUI) such as Microsoft Windows, files are shown as unique icons that relate to the program that opens the file.
  • 3. FILE SYSTEM : magic constants • MAGIC CONSTANTS • __FILE__ // The full path and filename of the file • __LINE__ // The current line number of the file. • __DIR__ // The directory of the file. (only php 5.3) • PHP FUNCTION • dirname(__FILE__); • File_exists(__FILE__); • File_exists(dirname(__FILE__) ) • File_exists(dirname(__FILE__).”/basic.html”) ? “yes”:”No” • Is_file() ; • Is_file(dirname(__FILE__).”/basic.html” ) ? “y”:”n” • Is_dir(); • Is_file(dirname(__FILE__) ) ? “y”:”n”
  • 4. UNDERSTANDING FILE PERMISSIONS: windows file permissions
  • 5. PHP AND FILE PERMISSIONS • Octal notation: 764
  • 6. PHP AND FILE PERMISSIONS • Chown() • Chown(‘file_name.php’,’owner_name’) ; • Chown only works in php is super user • Making webserver/PHP a super user is a big issue • Chmod(); • Fileperms(); • Fileperms(‘file_name.php’) // i.e 103306 • Decoct(); • Decoct(fileperms(‘file_name.php’)) • Sprintf() • Is_readable(); • Is_writable();
  • 7. ACCESSING FILES • 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 — • fread — • filesize — Gets file size • fgets — Gets line from file pointer
  • 8. ACCESSING FILES • ACCESSING FILES • fopen( filename , mode ); • Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use n, Windows systems use rn, and Macintosh systems use r as the line ending character.
  • 9. ACCESSING FILES 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.
  • 10. $file = ‘file-name.txt’; $handle = fopen( $file , ‘w’) fclose( $handle ); If ( $handle = fopen( $file , ‘w’)){ flose($handle ); } ACCESSING FILES
  • 11. $file = ‘file-name.txt’; If($handle = fopen( $file , ‘w’) ) { overwrite Fwrite( $file , “abc”); Fwrite( $file , ‘123’) ; Fwrite ( $file , ‘abcn123’) ; } WRITING TO FILES
  • 12. • 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. READING FILES
  • 13. if( $handle = fopen( $file , 'r') ){ //$filesize = filesize( $file ); //$contents = fread( $handle ,$filesize ); //echo fgets( $handle); while(!feof( $handle )){ $contents .= fgets( $handle ); } } echo $contents ; echo nl2br($contents); READING FILES
  • 14. $file = ‘filetext.txt’; If($handle = fopen( $file , ‘w’)){ Fwrite($handle , “123n456n789”); $pos = ftell ($handle); Fwrite( $handle , ‘abcdef’); Rewind($handle ); Fwrite( $handle , ‘xyz’); Fclose($handle); // beware, it will overtype!!! //Note : a and a+ modes will not let you move the pointer } MOVING THE FILE POINTER
  • 15. • File_put_conents : shortcut for fopen / fwrite / fclose $file = ‘filetest.txt’; $content = “111n222n333”; if( $size = file_put_contents( $file , $conent )){ Echo “a file of {$sie} bytes was created ”; } WRITING TO FILES
  • 16. • Close files first • Cant delete open files • Must have permission on folder containing the file Unlink(‘file_name’); DELETING FILES
  • 17. • getcwd — Gets the current working directory. • rmdir — Removes directory. • mkdir — Makes directory. • chdir — Change directory. • rename — Renames a file or directory. • readdir — Read entry from directory handle. • opendir — Open directory handle. • pathinfo — Returns information about a file path. WORKING WITH DIRECTORIES
  • 18. • readfile — Reads the contents of a file and returns the number of bytes read on success Other PHP File System Functions