PHP certification|PHP CERTIFICATION Coimbatore|PHP certification course in Coimbatore
PHP Filing
Click here to load reader
-
Upload
nisa-soomro -
Category
Education
-
view
320 -
download
0
description
Transcript of PHP Filing
![Page 1: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/1.jpg)
PHP Filing Instructor: Nisa Soomro
![Page 2: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/2.jpg)
Outlines• File manipulation using PHP
• Basic protocols
• Cookies and Sessions
![Page 3: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/3.jpg)
Introduction• Manipulating files is a basic necessity
for serious programmers and PHP
gives you a great deal of tools for
creating, uploading, and editing files.
• This is one of the most fundamental
subjects of server side programming
in general. Files are used in web
applications of all sizes.
![Page 4: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/4.jpg)
Creating a File• In PHP the fopen function is used to open
files. However, it can also create a file if it
does not find the file specified in the
function call. So if you use fopen on a file
that does not exist, it will create it, given that
you open the file for writing or appending.
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or
die("can't open file");
fclose($ourFileHandle);
![Page 5: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/5.jpg)
File Operations Mode
![Page 6: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/6.jpg)
Opening a File• The fopen() function is used to open
files in PHP.
<?php
$file = fopen("welcome.txt", "r") or
exit("Unable to open file!");
fclose($file);
![Page 7: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/7.jpg)
Reading From a File• The fgets() function is used to read a
single line from a file.<?php
$file = fopen("welcome.txt", "r") or
exit("Unable to open file!");
while(!feof($file))
{
echo fgets($file). "<br>";
}
fclose($file);
![Page 8: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/8.jpg)
Writing to a File• We can use php to write to a text file.
The fwrite function allows data to be
written to any type of file.
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or
die("can't open file");
$stringData = “Hello 13BS(CS)";
fwrite($fh, $stringData);
fclose($fh);
![Page 9: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/9.jpg)
Removing File• In PHP you delete files by calling
the unlink function.
$myFile = "testFile.txt";
unlink($myFile);
![Page 10: PHP Filing](https://reader038.fdocuments.in/reader038/viewer/2022100603/55943fe71a28abd85b8b47f3/html5/thumbnails/10.jpg)
Appending Data
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or
die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);