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

What's hot (17)

PPT
Filing system in PHP
Mudasir Syed
 
PPT
file
teach4uin
 
PPT
File handling in c
Vikash Dhal
 
PPT
05 File Handling Upload Mysql
Geshan Manandhar
 
PPT
File in c
Prabhu Govind
 
PPT
File handling-c
CGC Technical campus,Mohali
 
PPT
File handling in 'C'
Gaurav Garg
 
PDF
Module 03 File Handling in C
Tushar B Kute
 
PPT
File handling in c
David Livingston J
 
PPT
File in C Programming
Sonya Akter Rupa
 
PPSX
C programming file handling
argusacademy
 
PPT
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
PPTX
File Management in C
Paurav Shah
 
PPTX
File in C language
Manash Kumar Mondal
 
PPTX
File handling in c
mohit biswal
 
Filing system in PHP
Mudasir Syed
 
file
teach4uin
 
File handling in c
Vikash Dhal
 
05 File Handling Upload Mysql
Geshan Manandhar
 
File in c
Prabhu Govind
 
File handling in 'C'
Gaurav Garg
 
Module 03 File Handling in C
Tushar B Kute
 
File handling in c
David Livingston J
 
File in C Programming
Sonya Akter Rupa
 
C programming file handling
argusacademy
 
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
File Management in C
Paurav Shah
 
File in C language
Manash Kumar Mondal
 
File handling in c
mohit biswal
 

Similar to php file uploading (20)

PPT
Php i basic chapter 4
Muhamad Al Imran
 
PDF
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
PPTX
Working of File System: An Overview in detail
jinijames109
 
PPTX
lecture 10.pptx
ITNet
 
PPSX
DIWE - File handling with PHP
Rasan Samarasinghe
 
PPTX
Ch3(working with file)
Chhom Karath
 
PPTX
PHP Filing
Nisa Soomro
 
PPTX
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
MasSam13
 
PPTX
File accessing modes in c
manojmanoj218596
 
PPTX
Chap 5 php files part 1
monikadeshmane
 
DOCX
Files nts
kalyani66
 
PPTX
PHP File Handling
Degu8
 
PPTX
File handling
NithyaNithyav
 
PPT
Phpwebdevelping
mohamed ashraf
 
PDF
File system
Gayane Aslanyan
 
DOCX
Php advance
Rattanjeet Singh
 
PPTX
File handing in C
shrishcg
 
PPT
Php
TSUBHASHRI
 
PPT
Php
TSUBHASHRI
 
Php i basic chapter 4
Muhamad Al Imran
 
Web Development Course: PHP lecture 3
Gheyath M. Othman
 
Working of File System: An Overview in detail
jinijames109
 
lecture 10.pptx
ITNet
 
DIWE - File handling with PHP
Rasan Samarasinghe
 
Ch3(working with file)
Chhom Karath
 
PHP Filing
Nisa Soomro
 
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
MasSam13
 
File accessing modes in c
manojmanoj218596
 
Chap 5 php files part 1
monikadeshmane
 
Files nts
kalyani66
 
PHP File Handling
Degu8
 
File handling
NithyaNithyav
 
Phpwebdevelping
mohamed ashraf
 
File system
Gayane Aslanyan
 
Php advance
Rattanjeet Singh
 
File handing in C
shrishcg
 
Ad

Recently uploaded (20)

PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PPTX
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PDF
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PDF
Open Source Milvus Vector Database v 2.6
Zilliz
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Open Source Milvus Vector Database v 2.6
Zilliz
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Ad

php file uploading

  • 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.