SlideShare a Scribd company logo
1 of 26
Introduction to FileIntroduction to File
Handling with PHPHandling with PHP
Files and PHPFiles and PHP
• File Handling
o Data Storage
• Though slower than a database
o Manipulating uploaded files
• From forms
o Creating Files for download
Open/Close a FileOpen/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 ModesFile 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 ExampleFile 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..?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 DataReading Data
• There are two main functions to read data:
• fgets($handle,$bytes)
o Reads up to $bytes of data, stops at newline or end of file (EOF)
• fread($handle,$bytes)
o Reads up to $bytes of data, stops at EOF.
Reading DataReading Data
• We need to be aware of the End Of File (EOF)
point..
• feof($handle)
o Whether the file has reached the EOF point. Returns true if have reached
EOF.
Data Reading ExampleData Reading Example
$handle = fopen('people.txt', 'r');
while (!feof($handle)) {
echo fgets($handle, 1024);
echo '<br />';
}
fclose($handle);
Data Reading ExampleData 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 ExampleData 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 ExampleData 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..File Open shortcuts..
• There are two ‘shortcut’ functions that don’t
require a file to be opened:
• $lines = file($filename)
o Reads entire file into an array with each line a separate entry in the array.
• $str = file_get_contents($filename)
o Reads entire file into a single string.
Writing DataWriting Data
• To write data to a file use:
• fwrite($handle,$data)
o Write $data to the file.
Data Writing ExampleData Writing Example
$handle = fopen('people.txt', 'a');
fwrite($handle, “nFred:Male”);
fclose($handle);
Data Writing ExampleData 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 OperationsOther File Operations
• Delete file
o unlink('filename');
• Rename (file or directory)
o rename('old name', 'new name');
• Copy file
o copy('source', 'destination');
• And many, many more!
o www.php.net/manual/en/ref.filesystem.php
Dealing With DirectoriesDealing With Directories
• Open a directory
o $handle = opendir('dirname');
• $handle 'points' to the directory
• Read contents of directory
o readdir($handle)
• Returns name of next file in directory
• Files are sorted as on filesystem
• Close a directory
o closedir($handle)
• Closes directory 'stream'
Directory ExampleDirectory Example
$handle = opendir('./');
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
closedir($handle);
Directory ExampleDirectory Example
$handle = opendir('./');
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
closedir($handle);
Open current directory
$handle = opendir('./');
Directory ExampleDirectory Example
$handle = opendir('./');
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
closedir($handle);
Whilst readdir() returns a name, loop
through directory contents, echoing
results
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
Directory ExampleDirectory Example
$handle = opendir('./');
while(false !== ($file=readdir($handle)))
{
echo "$file<br />";
}
closedir($handle); Close the directory streamclosedir($handle);
Other DirectoryOther Directory
OperationsOperations
• Get current directory
o getcwd()
• Change Directory
o chdir('dirname');
• Create directory
o mkdir('dirname');
• Delete directory (MUST be empty)
o rmdir('dirname');
ReviewReview
• 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.
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/php-classes-in-mumbai.html

More Related Content

What's hot (20)

Php.ppt
Php.pptPhp.ppt
Php.ppt
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
Php array
Php arrayPhp array
Php array
 
jQuery
jQueryjQuery
jQuery
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Php string function
Php string function Php string function
Php string function
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side Programming
 
Php forms
Php formsPhp forms
Php forms
 
Directory structure
Directory structureDirectory structure
Directory structure
 
Xml
XmlXml
Xml
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
PHP slides
PHP slidesPHP slides
PHP slides
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 

Viewers also liked

Php File Operations
Php File OperationsPhp File Operations
Php File Operationsmussawir20
 
Opslag van long tail producten in e-warehouse
Opslag van long tail producten in e-warehouseOpslag van long tail producten in e-warehouse
Opslag van long tail producten in e-warehouseFréderique Debecker
 
Industrial measurement and automation - ADDI-DATA
Industrial measurement and automation  - ADDI-DATAIndustrial measurement and automation  - ADDI-DATA
Industrial measurement and automation - ADDI-DATAAddi-Data
 
HealthPort Technologies interview questions and answers
HealthPort Technologies interview questions and answersHealthPort Technologies interview questions and answers
HealthPort Technologies interview questions and answerstmegan693
 
FY15 COCOMs Sales Opportunities
FY15 COCOMs Sales OpportunitiesFY15 COCOMs Sales Opportunities
FY15 COCOMs Sales OpportunitiesimmixGroup
 
Avoiding Counterfeit Risk: How to mitigate part and supplier risk
Avoiding Counterfeit Risk: How to mitigate part and supplier risk Avoiding Counterfeit Risk: How to mitigate part and supplier risk
Avoiding Counterfeit Risk: How to mitigate part and supplier risk IHS
 
K-SIM Usage and Techniques for Simulating KEMET Capacitors
K-SIM Usage and Techniques for Simulating KEMET CapacitorsK-SIM Usage and Techniques for Simulating KEMET Capacitors
K-SIM Usage and Techniques for Simulating KEMET CapacitorsKEMET Electronics Corporation
 
IT-AAC Defense IT Reform Report to the Sec 809 Panel
IT-AAC Defense IT Reform Report to the Sec 809 PanelIT-AAC Defense IT Reform Report to the Sec 809 Panel
IT-AAC Defense IT Reform Report to the Sec 809 PanelJohn Weiler
 
Aerospace Elite Quality Assurance Presentation 2012
Aerospace Elite Quality Assurance Presentation 2012Aerospace Elite Quality Assurance Presentation 2012
Aerospace Elite Quality Assurance Presentation 2012nakenatter
 
Crimp Interconnect Systems for Printed Electronics
Crimp Interconnect Systems for Printed ElectronicsCrimp Interconnect Systems for Printed Electronics
Crimp Interconnect Systems for Printed ElectronicsPhil Heft
 
ERAI -Counterfeit Awareness-Avoidance Training
ERAI -Counterfeit Awareness-Avoidance Training ERAI -Counterfeit Awareness-Avoidance Training
ERAI -Counterfeit Awareness-Avoidance Training Kristal Snider
 
Global Technology Resources interview questions and answers
Global Technology Resources interview questions and answersGlobal Technology Resources interview questions and answers
Global Technology Resources interview questions and answersnydesti930
 
Military Aerospace Presentation
Military Aerospace PresentationMilitary Aerospace Presentation
Military Aerospace Presentationbkohlmeyer
 
Intership Report for printing
Intership Report for printingIntership Report for printing
Intership Report for printingOmar Javed
 
Corrosion removal capitulo 4
Corrosion removal capitulo 4Corrosion removal capitulo 4
Corrosion removal capitulo 4Luis Ulakia
 

Viewers also liked (20)

Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Opslag van long tail producten in e-warehouse
Opslag van long tail producten in e-warehouseOpslag van long tail producten in e-warehouse
Opslag van long tail producten in e-warehouse
 
The mindset of an alpha marketer
The mindset of an alpha marketerThe mindset of an alpha marketer
The mindset of an alpha marketer
 
Jayadeep cv latest
Jayadeep cv latestJayadeep cv latest
Jayadeep cv latest
 
Industrial measurement and automation - ADDI-DATA
Industrial measurement and automation  - ADDI-DATAIndustrial measurement and automation  - ADDI-DATA
Industrial measurement and automation - ADDI-DATA
 
HealthPort Technologies interview questions and answers
HealthPort Technologies interview questions and answersHealthPort Technologies interview questions and answers
HealthPort Technologies interview questions and answers
 
FY15 COCOMs Sales Opportunities
FY15 COCOMs Sales OpportunitiesFY15 COCOMs Sales Opportunities
FY15 COCOMs Sales Opportunities
 
Avoiding Counterfeit Risk: How to mitigate part and supplier risk
Avoiding Counterfeit Risk: How to mitigate part and supplier risk Avoiding Counterfeit Risk: How to mitigate part and supplier risk
Avoiding Counterfeit Risk: How to mitigate part and supplier risk
 
K-SIM Usage and Techniques for Simulating KEMET Capacitors
K-SIM Usage and Techniques for Simulating KEMET CapacitorsK-SIM Usage and Techniques for Simulating KEMET Capacitors
K-SIM Usage and Techniques for Simulating KEMET Capacitors
 
IT-AAC Defense IT Reform Report to the Sec 809 Panel
IT-AAC Defense IT Reform Report to the Sec 809 PanelIT-AAC Defense IT Reform Report to the Sec 809 Panel
IT-AAC Defense IT Reform Report to the Sec 809 Panel
 
Fraser Uniquip
Fraser UniquipFraser Uniquip
Fraser Uniquip
 
Aerospace Elite Quality Assurance Presentation 2012
Aerospace Elite Quality Assurance Presentation 2012Aerospace Elite Quality Assurance Presentation 2012
Aerospace Elite Quality Assurance Presentation 2012
 
Crimp Interconnect Systems for Printed Electronics
Crimp Interconnect Systems for Printed ElectronicsCrimp Interconnect Systems for Printed Electronics
Crimp Interconnect Systems for Printed Electronics
 
esi profile
esi profileesi profile
esi profile
 
ERAI -Counterfeit Awareness-Avoidance Training
ERAI -Counterfeit Awareness-Avoidance Training ERAI -Counterfeit Awareness-Avoidance Training
ERAI -Counterfeit Awareness-Avoidance Training
 
Swati Dubey QA 6 Yrs
Swati Dubey QA 6 YrsSwati Dubey QA 6 Yrs
Swati Dubey QA 6 Yrs
 
Global Technology Resources interview questions and answers
Global Technology Resources interview questions and answersGlobal Technology Resources interview questions and answers
Global Technology Resources interview questions and answers
 
Military Aerospace Presentation
Military Aerospace PresentationMilitary Aerospace Presentation
Military Aerospace Presentation
 
Intership Report for printing
Intership Report for printingIntership Report for printing
Intership Report for printing
 
Corrosion removal capitulo 4
Corrosion removal capitulo 4Corrosion removal capitulo 4
Corrosion removal capitulo 4
 

Similar to PHP - Introduction to File Handling with PHP

Similar to PHP - Introduction to File Handling with PHP (20)

Files
FilesFiles
Files
 
Files
FilesFiles
Files
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
Php i basic chapter 4
Php i basic chapter 4Php i basic chapter 4
Php i basic chapter 4
 
File system
File systemFile system
File system
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
 
PHP Filing
PHP Filing PHP Filing
PHP Filing
 
file management in c language
file management in c languagefile management in c language
file management in c language
 
Php files
Php filesPhp files
Php files
 
File handling in c
File handling in cFile handling in c
File handling in c
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
DIWE - File handling with PHP
DIWE - File handling with PHPDIWE - File handling with PHP
DIWE - File handling with PHP
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
File Handling in C Programming for Beginners
File Handling in C Programming for BeginnersFile Handling in C Programming for Beginners
File Handling in C Programming for Beginners
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 

More from Vibrant Technologies & Computers

Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Vibrant Technologies & Computers
 

More from Vibrant Technologies & Computers (20)

Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
SQL- Introduction to SQL database
SQL- Introduction to SQL database SQL- Introduction to SQL database
SQL- Introduction to SQL database
 
ITIL - introduction to ITIL
ITIL - introduction to ITILITIL - introduction to ITIL
ITIL - introduction to ITIL
 
Salesforce - Introduction to Security & Access
Salesforce -  Introduction to Security & Access Salesforce -  Introduction to Security & Access
Salesforce - Introduction to Security & Access
 
Data ware housing- Introduction to olap .
Data ware housing- Introduction to  olap .Data ware housing- Introduction to  olap .
Data ware housing- Introduction to olap .
 
Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.
 
Data ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housingData ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housing
 
Salesforce - classification of cloud computing
Salesforce - classification of cloud computingSalesforce - classification of cloud computing
Salesforce - classification of cloud computing
 
Salesforce - cloud computing fundamental
Salesforce - cloud computing fundamentalSalesforce - cloud computing fundamental
Salesforce - cloud computing fundamental
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
SQL- Introduction to advanced sql concepts
SQL- Introduction to  advanced sql conceptsSQL- Introduction to  advanced sql concepts
SQL- Introduction to advanced sql concepts
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
SQL- Introduction to SQL Set Operations
SQL- Introduction to SQL Set OperationsSQL- Introduction to SQL Set Operations
SQL- Introduction to SQL Set Operations
 
Sas - Introduction to designing the data mart
Sas - Introduction to designing the data martSas - Introduction to designing the data mart
Sas - Introduction to designing the data mart
 
Sas - Introduction to working under change management
Sas - Introduction to working under change managementSas - Introduction to working under change management
Sas - Introduction to working under change management
 
SAS - overview of SAS
SAS - overview of SASSAS - overview of SAS
SAS - overview of SAS
 
Teradata - Architecture of Teradata
Teradata - Architecture of TeradataTeradata - Architecture of Teradata
Teradata - Architecture of Teradata
 
Teradata - Restoring Data
Teradata - Restoring Data Teradata - Restoring Data
Teradata - Restoring Data
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 

Recently uploaded (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 

PHP - Introduction to File Handling with PHP

  • 1.
  • 2. Introduction to FileIntroduction to File Handling with PHPHandling with PHP
  • 3. Files and PHPFiles and PHP • File Handling o Data Storage • Though slower than a database o Manipulating uploaded files • From forms o Creating Files for download
  • 4. Open/Close a FileOpen/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.
  • 5. File Open ModesFile 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.
  • 6. File Open/Close ExampleFile 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); ?>
  • 7. Now what..?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..
  • 8. Reading DataReading Data • There are two main functions to read data: • fgets($handle,$bytes) o Reads up to $bytes of data, stops at newline or end of file (EOF) • fread($handle,$bytes) o Reads up to $bytes of data, stops at EOF.
  • 9. Reading DataReading Data • We need to be aware of the End Of File (EOF) point.. • feof($handle) o Whether the file has reached the EOF point. Returns true if have reached EOF.
  • 10. Data Reading ExampleData Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo '<br />'; } fclose($handle);
  • 11. Data Reading ExampleData 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');
  • 12. Data Reading ExampleData 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 />'; }
  • 13. Data Reading ExampleData Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo '<br />'; } fclose($handle); Close the file fclose($handle);
  • 14. File Open shortcuts..File Open shortcuts.. • There are two ‘shortcut’ functions that don’t require a file to be opened: • $lines = file($filename) o Reads entire file into an array with each line a separate entry in the array. • $str = file_get_contents($filename) o Reads entire file into a single string.
  • 15. Writing DataWriting Data • To write data to a file use: • fwrite($handle,$data) o Write $data to the file.
  • 16. Data Writing ExampleData Writing Example $handle = fopen('people.txt', 'a'); fwrite($handle, “nFred:Male”); fclose($handle);
  • 17. Data Writing ExampleData 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)
  • 18. Other File OperationsOther File Operations • Delete file o unlink('filename'); • Rename (file or directory) o rename('old name', 'new name'); • Copy file o copy('source', 'destination'); • And many, many more! o www.php.net/manual/en/ref.filesystem.php
  • 19. Dealing With DirectoriesDealing With Directories • Open a directory o $handle = opendir('dirname'); • $handle 'points' to the directory • Read contents of directory o readdir($handle) • Returns name of next file in directory • Files are sorted as on filesystem • Close a directory o closedir($handle) • Closes directory 'stream'
  • 20. Directory ExampleDirectory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file<br />"; } closedir($handle);
  • 21. Directory ExampleDirectory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file<br />"; } closedir($handle); Open current directory $handle = opendir('./');
  • 22. Directory ExampleDirectory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file<br />"; } closedir($handle); Whilst readdir() returns a name, loop through directory contents, echoing results while(false !== ($file=readdir($handle))) { echo "$file<br />"; }
  • 23. Directory ExampleDirectory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file<br />"; } closedir($handle); Close the directory streamclosedir($handle);
  • 24. Other DirectoryOther Directory OperationsOperations • Get current directory o getcwd() • Change Directory o chdir('dirname'); • Create directory o mkdir('dirname'); • Delete directory (MUST be empty) o rmdir('dirname');
  • 25. ReviewReview • 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.
  • 26. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/php-classes-in-mumbai.html