SlideShare a Scribd company logo
1 of 13
Download to read offline
WEB DEVELOPMENT AND
APPLICATIONS
PHP (Hypertext Preprocessor)
By: Gheyath M. Othman
PHP File Handling
File handling is an important part of any web application. You often need to
open and process a file for different tasks.
Manipulating Files
PHP has several functions for creating, reading, uploading, and editing files.
Be careful when manipulating files!
When you are manipulating files you must be very careful. You can do a lot of
damage if you do something wrong. Common errors are: editing the wrong file,
filling a hard-drive with garbage data, and deleting the content of a file by
accident
PHP File Handling
PHP readfile() Function
The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called "webdictionary.txt", stored on the server,
that looks like this:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
Example
<!DOCTYPE html><html><body>
<?php
echo readfile("webdictionary.txt");
?>
</body></html>
PHP file
webdictionary.txt
Note: It will display no. of characters
at the end
PHP File Open/Read/Close
PHP Open File - fopen()
A better method to open files is with the fopen() function. This function gives
you more options than the readfile() function.
Example: using same text file
The first parameter of fopen() contains the name of the file to be opened and
the second parameter specifies in which mode the file should be opened.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
PHP File Open/Read/Close
The file may be opened in one of the following modes:
Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist.
File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of
the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist.
File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of
the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
PHP File Open/Read/Close
PHP Read File - fread()
The fread() function reads from an open file.
Example:
<!DOCTYPE html><html><body>
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable
to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
</body></html>
The first parameter of fread() contains the name of the file to read from and
the second parameter specifies the maximum number of bytes to read.
Maximum number of characters to be read
You can use a number instead
PHP File Open/Read/Close
<!DOCTYPE html><html><body>
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable
to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
</body></html>
PHP Close File - fclose()
The fclose() function is used to close an open file.
The fclose() requires the name of the file (or a variable that holds the filename)
we want to close
NOTE: It's a good programming practice to close all files after you have
finished with them. You don't want an open file running around on your server
taking up resources!
PHP File Open/Read/Close
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable
to open file!");
echo fgets($myfile);
fclose($myfile);
?>
PHP Read Single Line - fgets()
The fgets() function is used to read a single line from a file.
The example below outputs the first line of the "webdictionary.txt" file:
NOTE: After a call to the fgets() function, the file pointer has moved to the next line.
PHP File Open/Read/Close
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP Check End-Of-File - feof()
• The feof() function checks if the "end-of-file" (EOF) has been reached.
• The feof() function is useful for looping through data of unknown length.
• The example below reads the "webdictionary.txt" file line by line, until end-of-
file is reached:
NOTE: After a call to the fgets() function, the file pointer has moved to the next line.
PHP File Open/Read/Close
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
PHP Read Single Character - fgetc()
• The fgetc() function is used to read a single character from a file.
• The example below reads the "webdictionary.txt" file character by character,
until end-of-file is reached:
NOTE: After a call to the fgetc() function, the file pointer moves to the next character.
PHP File Create/Write
PHP Create File - fopen()
• The fopen() function is also used to create a file. Maybe a little confusing, but
in PHP, a file is created using the same function used to open files.
• If you use fopen() on a file that does not exist, it will create it, given that the
file is opened for writing (w) or appending (a).
• The example below creates a new file called "testfile.txt". The file will be
created in the same directory where the PHP code resides.
$myfile = fopen("testfile.txt", "w")
PHP File Create/Write
PHP Write to File - fwrite()
• The fwrite() function is used to write to a file.
• The first parameter of fwrite() contains the name of the file to write to and the
second parameter is the string to be written.
• The example below writes a couple of names into a new file called newfile.txt":
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = “Kurdistan Rayann";
fwrite($myfile, $txt);
$txt = “Darin Rayann";
fwrite($myfile, $txt);
fclose($myfile);
?>
If we open the "newfile.txt" file it would look like this:
Kurdistan Rayan
Darin Rayan
PHP File Create/Write
PHP Overwriting
• Now that "newfile.txt" contains some data we can show what happens when
we open an existing file for writing. All the existing data will be ERASED and we
start with an empty file.
• In the example below we open our existing file "newfile.txt", and write some
new data into it:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = “Arin Kurdi“. PHP_EOL;
fwrite($myfile, $txt);
$txt = “Aryan Kurdi“.PHP_EOL;
fwrite($myfile, $txt);
fclose($myfile);
?>
Arin Kurdi
Aryan Kurdi
If we now open the "newfile.txt" file, both Kurdistan and
Darin have vanished, and only the data we just wrote is
present:
You can use the function
PHP_EOL to break a line
Which is better than n
$txt = “Arin Kurdi“.PHP_EOL;

More Related Content

What's hot

What's hot (20)

Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql05 File Handling Upload Mysql
05 File Handling Upload Mysql
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
Unit 1 php_basics
Unit 1 php_basicsUnit 1 php_basics
Unit 1 php_basics
 
Php
PhpPhp
Php
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
File Upload
File UploadFile Upload
File Upload
 
File upload php
File upload phpFile upload php
File upload php
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 

Similar to Web Development Course: PHP lecture 3

PHP File Handling
PHP File Handling PHP File Handling
PHP File Handling Degu8
 
lecture 10.pptx
lecture 10.pptxlecture 10.pptx
lecture 10.pptxITNet
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptRaja Ram Dutta
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHPMudasir Syed
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16Vishal Dutt
 
Python files / directories part15
Python files / directories  part15Python files / directories  part15
Python files / directories part15Vishal Dutt
 
Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptxAdnan al-emran
 
Php file handling in Hindi
Php file handling in Hindi Php file handling in Hindi
Php file handling in Hindi Vipin sharma
 

Similar to Web Development Course: PHP lecture 3 (20)

Files in php
Files in phpFiles in php
Files in php
 
PHP File Handling
PHP File Handling PHP File Handling
PHP File Handling
 
DIWE - File handling with PHP
DIWE - File handling with PHPDIWE - File handling with PHP
DIWE - File handling with PHP
 
Php files
Php filesPhp files
Php files
 
Php advance
Php advancePhp advance
Php advance
 
PHP Filing
PHP Filing PHP Filing
PHP Filing
 
lecture 10.pptx
lecture 10.pptxlecture 10.pptx
lecture 10.pptx
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
File accessing modes in c
File accessing modes in cFile accessing modes in c
File accessing modes in c
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
Python files / directories part16
Python files / directories  part16Python files / directories  part16
Python files / directories part16
 
Python files / directories part15
Python files / directories  part15Python files / directories  part15
Python files / directories part15
 
Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptx
 
Php file handling in Hindi
Php file handling in Hindi Php file handling in Hindi
Php file handling in Hindi
 

More from Gheyath M. Othman

Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1Gheyath M. Othman
 
Web Design Course: CSS lecture 5
Web Design Course: CSS  lecture 5Web Design Course: CSS  lecture 5
Web Design Course: CSS lecture 5Gheyath M. Othman
 
Web Design Course: CSS lecture 4
Web Design Course: CSS  lecture 4Web Design Course: CSS  lecture 4
Web Design Course: CSS lecture 4Gheyath M. Othman
 
Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3Gheyath M. Othman
 
Web Design Course: CSS lecture 2
Web Design Course: CSS  lecture 2Web Design Course: CSS  lecture 2
Web Design Course: CSS lecture 2Gheyath M. Othman
 
Web Design Course: CSS lecture 6
Web Design Course: CSS  lecture 6Web Design Course: CSS  lecture 6
Web Design Course: CSS lecture 6Gheyath M. Othman
 

More from Gheyath M. Othman (7)

Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1
 
Web Design Course: CSS lecture 5
Web Design Course: CSS  lecture 5Web Design Course: CSS  lecture 5
Web Design Course: CSS lecture 5
 
Web Design Course: CSS lecture 4
Web Design Course: CSS  lecture 4Web Design Course: CSS  lecture 4
Web Design Course: CSS lecture 4
 
Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3
 
Web Design Course: CSS lecture 2
Web Design Course: CSS  lecture 2Web Design Course: CSS  lecture 2
Web Design Course: CSS lecture 2
 
Web Design Course: CSS lecture 6
Web Design Course: CSS  lecture 6Web Design Course: CSS  lecture 6
Web Design Course: CSS lecture 6
 

Recently uploaded

Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation SlidesKeppelCorporation
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...lizamodels9
 
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckPitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckHajeJanKamps
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Timedelhimodelshub1
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?Olivia Kresic
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...lizamodels9
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCRashishs7044
 
Contemporary Economic Issues Facing the Filipino Entrepreneur (1).pptx
Contemporary Economic Issues Facing the Filipino Entrepreneur (1).pptxContemporary Economic Issues Facing the Filipino Entrepreneur (1).pptx
Contemporary Economic Issues Facing the Filipino Entrepreneur (1).pptxMarkAnthonyAurellano
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesKeppelCorporation
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
Future Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionFuture Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionMintel Group
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCRashishs7044
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...lizamodels9
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
Marketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet CreationsMarketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet Creationsnakalysalcedo61
 
Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024Kirill Klimov
 

Recently uploaded (20)

Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
 
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckPitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Time
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR
 
Contemporary Economic Issues Facing the Filipino Entrepreneur (1).pptx
Contemporary Economic Issues Facing the Filipino Entrepreneur (1).pptxContemporary Economic Issues Facing the Filipino Entrepreneur (1).pptx
Contemporary Economic Issues Facing the Filipino Entrepreneur (1).pptx
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation Slides
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
Future Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionFuture Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted Version
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
Marketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet CreationsMarketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet Creations
 
Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024Flow Your Strategy at Flight Levels Day 2024
Flow Your Strategy at Flight Levels Day 2024
 

Web Development Course: PHP lecture 3

  • 1. WEB DEVELOPMENT AND APPLICATIONS PHP (Hypertext Preprocessor) By: Gheyath M. Othman
  • 2. PHP File Handling File handling is an important part of any web application. You often need to open and process a file for different tasks. Manipulating Files PHP has several functions for creating, reading, uploading, and editing files. Be careful when manipulating files! When you are manipulating files you must be very careful. You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident
  • 3. PHP File Handling PHP readfile() Function The readfile() function reads a file and writes it to the output buffer. Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this: AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language Example <!DOCTYPE html><html><body> <?php echo readfile("webdictionary.txt"); ?> </body></html> PHP file webdictionary.txt Note: It will display no. of characters at the end
  • 4. PHP File Open/Read/Close PHP Open File - fopen() A better method to open files is with the fopen() function. This function gives you more options than the readfile() function. Example: using same text file The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?>
  • 5. PHP File Open/Read/Close The file may be opened in one of the following modes: Modes Description r Open a file for read only. File pointer starts at the beginning of the file w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist x Creates a new file for write only. Returns FALSE and an error if file already exists r+ Open a file for read/write. File pointer starts at the beginning of the file w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
  • 6. PHP File Open/Read/Close PHP Read File - fread() The fread() function reads from an open file. Example: <!DOCTYPE html><html><body> <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?> </body></html> The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read. Maximum number of characters to be read You can use a number instead
  • 7. PHP File Open/Read/Close <!DOCTYPE html><html><body> <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?> </body></html> PHP Close File - fclose() The fclose() function is used to close an open file. The fclose() requires the name of the file (or a variable that holds the filename) we want to close NOTE: It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!
  • 8. PHP File Open/Read/Close <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fgets($myfile); fclose($myfile); ?> PHP Read Single Line - fgets() The fgets() function is used to read a single line from a file. The example below outputs the first line of the "webdictionary.txt" file: NOTE: After a call to the fgets() function, the file pointer has moved to the next line.
  • 9. PHP File Open/Read/Close <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); // Output one line until end-of-file while(!feof($myfile)) { echo fgets($myfile) . "<br>"; } fclose($myfile); ?> PHP Check End-Of-File - feof() • The feof() function checks if the "end-of-file" (EOF) has been reached. • The feof() function is useful for looping through data of unknown length. • The example below reads the "webdictionary.txt" file line by line, until end-of- file is reached: NOTE: After a call to the fgets() function, the file pointer has moved to the next line.
  • 10. PHP File Open/Read/Close <?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); // Output one character until end-of-file while(!feof($myfile)) { echo fgetc($myfile); } fclose($myfile); ?> PHP Read Single Character - fgetc() • The fgetc() function is used to read a single character from a file. • The example below reads the "webdictionary.txt" file character by character, until end-of-file is reached: NOTE: After a call to the fgetc() function, the file pointer moves to the next character.
  • 11. PHP File Create/Write PHP Create File - fopen() • The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. • If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a). • The example below creates a new file called "testfile.txt". The file will be created in the same directory where the PHP code resides. $myfile = fopen("testfile.txt", "w")
  • 12. PHP File Create/Write PHP Write to File - fwrite() • The fwrite() function is used to write to a file. • The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written. • The example below writes a couple of names into a new file called newfile.txt": <?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = “Kurdistan Rayann"; fwrite($myfile, $txt); $txt = “Darin Rayann"; fwrite($myfile, $txt); fclose($myfile); ?> If we open the "newfile.txt" file it would look like this: Kurdistan Rayan Darin Rayan
  • 13. PHP File Create/Write PHP Overwriting • Now that "newfile.txt" contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file. • In the example below we open our existing file "newfile.txt", and write some new data into it: <?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = “Arin Kurdi“. PHP_EOL; fwrite($myfile, $txt); $txt = “Aryan Kurdi“.PHP_EOL; fwrite($myfile, $txt); fclose($myfile); ?> Arin Kurdi Aryan Kurdi If we now open the "newfile.txt" file, both Kurdistan and Darin have vanished, and only the data we just wrote is present: You can use the function PHP_EOL to break a line Which is better than n $txt = “Arin Kurdi“.PHP_EOL;