SlideShare a Scribd company logo
File Handling with PHP
Files and PHP File Handling Data Storage Though slower than a database Manipulating uploaded files From forms Creating Files for download
Open/Close a File A file is opened with  fopen () as a “stream”, and PHP returns a ‘handle’ to the file that can be used to reference the open file in other functions. Each file is opened in a particular  mode . A file is closed with  fclose () or when your script ends.
File Open Modes ‘ r’ Open for reading only. Start at beginning of file. ‘ r+’ Open for reading and writing. Start at beginning of file. ‘ w’ Open for writing only. Remove all previous content, if file doesn’t exist, create it. ‘ a’ Open writing, but start at END of current content. ‘ a+’ Open for reading and writing, start at END and create file if necessary.
File Open/Close Example <?php // open file to read $toread =  fopen ( ‘some/file.ext’ , ’r’ ); // open (possibly new) file to write $towrite =  fopen ( ‘some/file.ext’ , ’w’ ); // close both files fclose ($toread); fclose ($towrite); ?>
Now what..? If you open a file to read, you can use more in-built PHP functions to read data.. If you open the file to write, you can use more in-built PHP functions to write..
Reading Data There are two main functions to read data: fgets ($handle,$bytes)   Reads up to $bytes of data, stops at newline or end of file (EOF) fread ($handle,$bytes)   Reads up to $bytes of data, stops at EOF.
Reading Data We need to be aware of the End Of File (EOF) point.. feof ($handle)   Whether the file has reached the EOF point. Returns true if have reached EOF.
Data Reading Example $handle =  fopen ( 'people.txt' ,  'r' ); while (! feof ($handle)) { echo  fgets ($handle,  1024 ); echo  '<br />' ; } fclose ($handle);
Data Reading Example $handle =  fopen ( 'people.txt' ,  'r' ); while (! feof ($handle)) { echo  fgets ($handle,  1024 ); echo  '<br />' ; } fclose ($handle); Open the file and assign the resource to $handle $handle =  fopen ( 'people.txt' ,  'r' );
Data Reading Example $handle =  fopen ( 'people.txt' ,  'r' ); while (! feof ($handle)) { echo  fgets ($handle,  1024 ); echo  '<br />' ; } fclose ($handle); While NOT at the end of the file, pointed to by $handle, get and echo the data line by line while (! feof ($handle)) { echo  fgets ($handle,  1024 ); echo  '<br />' ; }
Data Reading Example $handle =  fopen ( 'people.txt' ,  'r' ); while (! feof ($handle)) { echo  fgets ($handle,  1024 ); echo  '<br />' ; } fclose ($handle); Close the file fclose ($handle);
File Open shortcuts.. There are two ‘shortcut’ functions that don’t require a file to be opened: $lines =  file ($filename)   Reads entire file into an array with each line a separate entry in the array. $str =  file_get_contents ($filename)   Reads entire file into a single string.
Writing Data To write data to a file use: fwrite ($handle,$data)   Write $data to the file.
Data Writing Example $handle =  fopen ( 'people.txt' ,  'a' ); fwrite ($handle,  “\nFred:Male” ); fclose ($handle);
Data Writing Example $handle =  fopen ( 'people.txt' ,  'a' ); fwrite ($handle,  '\nFred:Male' ); fclose ($handle); $handle =  fopen ( 'people.txt' ,  'a' ); Open file to append data (mode 'a')  fwrite ($handle,  “\nFred:Male” ); Write new data (with line break after previous data)
Other File Operations Delete file unlink ( 'filename' ); Rename (file or directory) rename ( 'old name' ,  'new name' ); Copy file copy ( 'source' ,  'destination' ); And many, many more! www.php.net/manual/en/ref.filesystem.php
Dealing With Directories Open a directory $handle =  opendir ('dirname'); $handle  'points' to the directory Read contents of directory readdir ($handle) Returns name of next file in directory Files are sorted as on filesystem Close a directory closedir ($handle) Closes directory 'stream'
Directory Example $handle =  opendir ( './' ); while ( false  !== ($file= readdir ($handle))) { echo   &quot;$file<br />&quot; ; } closedir ($handle);
Directory Example $handle =  opendir ( './' ); while ( false  !== ($file= readdir ($handle))) { echo   &quot;$file<br />&quot; ; } closedir ($handle); Open current directory $handle =  opendir ( './' );
Directory Example $handle =  opendir ( './' ); while ( false  !== ($file= readdir ($handle))) { echo   &quot;$file<br />&quot; ; } closedir ($handle); Whilst readdir() returns a name, loop through directory contents, echoing results while ( false  !== ($file= readdir ($handle))) { echo   &quot;$file<br />&quot; ; }
Directory Example $handle =  opendir ( './' ); while ( false  !== ($file= readdir ($handle))) { echo   &quot;$file<br />&quot; ; } closedir ($handle); Close the directory stream closedir ($handle);
Other Directory Operations Get current directory getcwd () Change Directory chdir ( 'dirname' ); Create directory mkdir ( 'dirname' ); Delete directory (MUST be empty) rmdir ( 'dirname' ); And more! www.php.net/manual/en/ref.dir.php
Review Can open and close files. Can read a file line by line or all at one go. Can write to files. Can open and cycle through the files in a directory.

More Related Content

PPT
php file uploading
Purushottam Kumar
 
PPTX
Php File Operations
Jamshid Hashimi
 
PPT
Php i basic chapter 4
Muhamad Al Imran
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PPT
Php File Operations
mussawir20
 
PDF
Php file handling in Hindi
Vipin sharma
 
PPT
Files and Directories in PHP
Nicole Ryan
 
DOCX
Php files
kalyani66
 
php file uploading
Purushottam Kumar
 
Php File Operations
Jamshid Hashimi
 
Php i basic chapter 4
Muhamad Al Imran
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Php File Operations
mussawir20
 
Php file handling in Hindi
Vipin sharma
 
Files and Directories in PHP
Nicole Ryan
 
Php files
kalyani66
 

What's hot (19)

PDF
PHP file handling
wahidullah mudaser
 
PPTX
Files in php
sana mateen
 
PPTX
Python data file handling
ToniyaP1
 
PPT
spug_2008-08
colinmeyer
 
RTF
Unix lab manual
Chaitanya Kn
 
PDF
Unix 1st sem lab programs a - VTU Karnataka
iCreateWorld
 
PPT
file
teach4uin
 
PDF
TDC São Paulo 2016 - Become a jedi with php streams
Matheus Marabesi
 
PPT
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
PPTX
File upload for the 21st century
Jiří Pudil
 
KEY
rdfapi.js and js3.js by webr3
ykskm
 
PPT
File handling
Ans Ali
 
PPT
File handling-c
CGC Technical campus,Mohali
 
PPT
File in c
Prabhu Govind
 
PPT
File handling in c
Vikash Dhal
 
PPT
File handling in c
David Livingston J
 
PPTX
Scripting 101
ohardebol
 
PPTX
Image upload in php MySql
Ishaq Shinwari
 
PHP file handling
wahidullah mudaser
 
Files in php
sana mateen
 
Python data file handling
ToniyaP1
 
spug_2008-08
colinmeyer
 
Unix lab manual
Chaitanya Kn
 
Unix 1st sem lab programs a - VTU Karnataka
iCreateWorld
 
file
teach4uin
 
TDC São Paulo 2016 - Become a jedi with php streams
Matheus Marabesi
 
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
File upload for the 21st century
Jiří Pudil
 
rdfapi.js and js3.js by webr3
ykskm
 
File handling
Ans Ali
 
File in c
Prabhu Govind
 
File handling in c
Vikash Dhal
 
File handling in c
David Livingston J
 
Scripting 101
ohardebol
 
Image upload in php MySql
Ishaq Shinwari
 
Ad

Viewers also liked (6)

DOCX
Mca java application projects completed list(is)
S3 Infotech IEEE Projects
 
PPT
SHOW104: Practical Java
Mark Myers
 
PPTX
Programming basics
illidari
 
PDF
Java practical(baca sem v)
mehul patel
 
PPTX
File Uploading in PHP
Idrees Hussain
 
DOCX
Java PRACTICAL file
RACHIT_GUPTA
 
Mca java application projects completed list(is)
S3 Infotech IEEE Projects
 
SHOW104: Practical Java
Mark Myers
 
Programming basics
illidari
 
Java practical(baca sem v)
mehul patel
 
File Uploading in PHP
Idrees Hussain
 
Java PRACTICAL file
RACHIT_GUPTA
 
Ad

Similar to Files (20)

PPT
Filing system in PHP
Mudasir Syed
 
PPT
file_handling_in_c.ppt
yuvrajkeshri
 
PPTX
Ch3(working with file)
Chhom Karath
 
PDF
File system
Gayane Aslanyan
 
PDF
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PPTX
PPS PPT 2.pptx
Sandeepbhuma1
 
PDF
IO Streams, Files and Directories
Krasimir Berov (Красимир Беров)
 
PPSX
DIWE - File handling with PHP
Rasan Samarasinghe
 
PPTX
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
harleensingh985
 
PPTX
File in C language
Manash Kumar Mondal
 
DOCX
java programming
Manoj Palbabu
 
PDF
File Handling.pdffile handling ppt final
e13225064
 
PPT
File handling-dutt
Anil Dutt
 
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
PPT
File handling in c
thirumalaikumar3
 
PDF
Module 03 File Handling in C
Tushar B Kute
 
ODP
Getting groovy (ODP)
Nick Dixon
 
PPT
PHP
webhostingguy
 
PPT
Unit5 C
arnold 7490
 
DOCX
Satz1
rajeshmhvr
 
Filing system in PHP
Mudasir Syed
 
file_handling_in_c.ppt
yuvrajkeshri
 
Ch3(working with file)
Chhom Karath
 
File system
Gayane Aslanyan
 
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PPS PPT 2.pptx
Sandeepbhuma1
 
IO Streams, Files and Directories
Krasimir Berov (Красимир Беров)
 
DIWE - File handling with PHP
Rasan Samarasinghe
 
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
harleensingh985
 
File in C language
Manash Kumar Mondal
 
java programming
Manoj Palbabu
 
File Handling.pdffile handling ppt final
e13225064
 
File handling-dutt
Anil Dutt
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
File handling in c
thirumalaikumar3
 
Module 03 File Handling in C
Tushar B Kute
 
Getting groovy (ODP)
Nick Dixon
 
Unit5 C
arnold 7490
 
Satz1
rajeshmhvr
 

Recently uploaded (20)

PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 

Files

  • 2. Files and PHP File Handling Data Storage Though slower than a database Manipulating uploaded files From forms Creating Files for download
  • 3. Open/Close a File A file is opened with fopen () as a “stream”, and PHP returns a ‘handle’ to the file that can be used to reference the open file in other functions. Each file is opened in a particular mode . A file is closed with fclose () or when your script ends.
  • 4. File Open Modes ‘ r’ Open for reading only. Start at beginning of file. ‘ r+’ Open for reading and writing. Start at beginning of file. ‘ w’ Open for writing only. Remove all previous content, if file doesn’t exist, create it. ‘ a’ Open writing, but start at END of current content. ‘ a+’ Open for reading and writing, start at END and create file if necessary.
  • 5. File Open/Close Example <?php // open file to read $toread = fopen ( ‘some/file.ext’ , ’r’ ); // open (possibly new) file to write $towrite = fopen ( ‘some/file.ext’ , ’w’ ); // close both files fclose ($toread); fclose ($towrite); ?>
  • 6. Now what..? If you open a file to read, you can use more in-built PHP functions to read data.. If you open the file to write, you can use more in-built PHP functions to write..
  • 7. Reading Data There are two main functions to read data: fgets ($handle,$bytes) Reads up to $bytes of data, stops at newline or end of file (EOF) fread ($handle,$bytes) Reads up to $bytes of data, stops at EOF.
  • 8. Reading Data We need to be aware of the End Of File (EOF) point.. feof ($handle) Whether the file has reached the EOF point. Returns true if have reached EOF.
  • 9. Data Reading Example $handle = fopen ( 'people.txt' , 'r' ); while (! feof ($handle)) { echo fgets ($handle, 1024 ); echo '<br />' ; } fclose ($handle);
  • 10. Data Reading Example $handle = fopen ( 'people.txt' , 'r' ); while (! feof ($handle)) { echo fgets ($handle, 1024 ); echo '<br />' ; } fclose ($handle); Open the file and assign the resource to $handle $handle = fopen ( 'people.txt' , 'r' );
  • 11. Data Reading Example $handle = fopen ( 'people.txt' , 'r' ); while (! feof ($handle)) { echo fgets ($handle, 1024 ); echo '<br />' ; } fclose ($handle); While NOT at the end of the file, pointed to by $handle, get and echo the data line by line while (! feof ($handle)) { echo fgets ($handle, 1024 ); echo '<br />' ; }
  • 12. Data Reading Example $handle = fopen ( 'people.txt' , 'r' ); while (! feof ($handle)) { echo fgets ($handle, 1024 ); echo '<br />' ; } fclose ($handle); Close the file fclose ($handle);
  • 13. File Open shortcuts.. There are two ‘shortcut’ functions that don’t require a file to be opened: $lines = file ($filename) Reads entire file into an array with each line a separate entry in the array. $str = file_get_contents ($filename) Reads entire file into a single string.
  • 14. Writing Data To write data to a file use: fwrite ($handle,$data) Write $data to the file.
  • 15. Data Writing Example $handle = fopen ( 'people.txt' , 'a' ); fwrite ($handle, “\nFred:Male” ); fclose ($handle);
  • 16. Data Writing Example $handle = fopen ( 'people.txt' , 'a' ); fwrite ($handle, '\nFred:Male' ); fclose ($handle); $handle = fopen ( 'people.txt' , 'a' ); Open file to append data (mode 'a') fwrite ($handle, “\nFred:Male” ); Write new data (with line break after previous data)
  • 17. Other File Operations Delete file unlink ( 'filename' ); Rename (file or directory) rename ( 'old name' , 'new name' ); Copy file copy ( 'source' , 'destination' ); And many, many more! www.php.net/manual/en/ref.filesystem.php
  • 18. Dealing With Directories Open a directory $handle = opendir ('dirname'); $handle 'points' to the directory Read contents of directory readdir ($handle) Returns name of next file in directory Files are sorted as on filesystem Close a directory closedir ($handle) Closes directory 'stream'
  • 19. Directory Example $handle = opendir ( './' ); while ( false !== ($file= readdir ($handle))) { echo &quot;$file<br />&quot; ; } closedir ($handle);
  • 20. Directory Example $handle = opendir ( './' ); while ( false !== ($file= readdir ($handle))) { echo &quot;$file<br />&quot; ; } closedir ($handle); Open current directory $handle = opendir ( './' );
  • 21. Directory Example $handle = opendir ( './' ); while ( false !== ($file= readdir ($handle))) { echo &quot;$file<br />&quot; ; } closedir ($handle); Whilst readdir() returns a name, loop through directory contents, echoing results while ( false !== ($file= readdir ($handle))) { echo &quot;$file<br />&quot; ; }
  • 22. Directory Example $handle = opendir ( './' ); while ( false !== ($file= readdir ($handle))) { echo &quot;$file<br />&quot; ; } closedir ($handle); Close the directory stream closedir ($handle);
  • 23. Other Directory Operations Get current directory getcwd () Change Directory chdir ( 'dirname' ); Create directory mkdir ( 'dirname' ); Delete directory (MUST be empty) rmdir ( 'dirname' ); And more! www.php.net/manual/en/ref.dir.php
  • 24. Review Can open and close files. Can read a file line by line or all at one go. Can write to files. Can open and cycle through the files in a directory.