LIS651 lecture 2 mySQL and PHP mySQL function Thomas Krichel 2005-11-04.
16-Filing lecture 1 php
-
Upload
raja-kumar -
Category
Education
-
view
480 -
download
4
description
Transcript of 16-Filing lecture 1 php
![Page 1: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/1.jpg)
File Handling
![Page 2: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/2.jpg)
What is File? A collection of data or information. One that has a name, called the filename. Almost all information stored in a computer must be in
a file. There are many different types of files:
data files
text files
program files
directory files and so on.
Different types of files store different types of information. For example, program files store programs, whereas text files store text.
![Page 3: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/3.jpg)
Tracks , Sectors And Clusters
Tracks The tracks are the thin concentric circular strips of
sectors. At least one head is required to read a single track.
Sectors A sector is the smallest storage unit that is addressable
by a hard drive, and all information stored by the hard drive is recorded in sectors.
Blocks and clusters The term block to refer to a sector or group of sectors.
![Page 4: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/4.jpg)
Files In Php
Php provides some functions for handling files. We can create, write , read, append, copy and their
permission can be set/done.
fopen — Opens file or URL fwrite — Binary-safe file write fread — Binary-safe file read filesize — Gets file size fgets — Gets line from file pointer feof — Returns true at end of file
![Page 5: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/5.jpg)
fopen The PHP fopen() function is used to open a file. It requires two
arguments stating first the file name and then mode in which to operate.
Files modes can be specified as one of the six options in this table.Mode Purpose
r • Opens the file for reading only.• Places the file pointer at the beginning of the file.
r+ • Opens the file for reading and writing.• Places the file pointer at the beginning of the file.
w • Opens the file for writing only.• Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does notexist then it attempts to create a file.
w+ • Opens the file for reading and writing only.• Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does notexist then it attemts to create a file.
a • Opens the file for writing only.• Places the file pointer at the end of the file.• If files does not exist then it attemts to create a file.
a+ • Opens the file for reading and writing only.• Places the file pointer at the end of the file.• If files does not exist then it attemts to create a file.
x • Create and open for writing only; place the file pointer at the beginning of the file. • If the file already exists, the fopen() call will fail by returning FALSE If the file
does not exist, attempt to create it.
X+ • Create and open for reading & writing only; place the file pointer at the beginning of the file.
• If the file already exists, the fopen() call will fail by returning FALSE If the file does not exist, attempt to create it.
![Page 6: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/6.jpg)
fwrite() A new file can be written or text can be appended to an
existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer The string of data that is to be written.
<?php$filename = “test.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
?>
![Page 7: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/7.jpg)
Reading A File
Once a file is opened using fopen() function it can be read with a function called fread().
This function requires two arguments: The file pointer The length of the file expressed in bytes.
The file's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.
So here are the steps required to read a file with PHP. Open a file using fopen() function. Get the file's length using filesize() function. Read the file's content using fread() function. Close the file with fclose() function.
![Page 8: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/8.jpg)
Magic Constants
__File__
__Line__
__DIR__
is_dir() — Tells whether the filename is a directory. is_file() — Tells whether the filename is a regular file. file_exists ()— Checks whether a file or directory exists file_get_contents(filename); file_put_contents(filename);
![Page 9: 16-Filing lecture 1 php](https://reader035.fdocuments.us/reader035/viewer/2022062511/54b7b2364a7959c9688b45e6/html5/thumbnails/9.jpg)
Questions?