Programming With PHP and MySQL-2
Programming With PHP and MySQL-2
FILE HANDLING
3.1 Opening files using fopen
fopen() function is Used to open the specified file.
fopen(filename, mode)
Filename- Required. Specifies the file or URL to open
Mode - Required. Specifies the type of access you require to the file/stream.
There are different types of mode are listed below:
3.2 Looping over a files content with
Feof()
The feof() function is used for looping through the content of a file if the
size of content is not known beforehand.
The feof() function returns True if end-of-file has been reached or if an
error has occurred. Else it returns False.
Syntax:
feof( $file )
Parameters: The feof() function in PHP accepts only one parameter which is $file.
This parameter specifies the file which has to be checked for end-of-file.
Return Value: It returns TRUE if end-of-file has been reached or if an error has
occurred. Else it returns False.
Example:
<?php
$test = stat('new.txt');
echo 'Access time: ' .$test['atime'];
echo '<br />Modification time: ' .$test['mtime'];
echo '<br />Device number: ' .$test['dev'];
?>
Output:
Access time: 1602748187
Modification time: 1603208669
Device number: 2
3.11 fseek( ) Function
The fseek() function in PHP is an inbuilt function which is used to seek in an
open file.
It moves the file pointer from its current position to a new position, forward
or backward specified by the number of bytes.
It returns 0 on success, else returns -1 on failure.
Syntax:
fseek ( $file, $offset, $whence)
Parameters: The fseek() function in PHP accepts three parameters as
described below.
$file: It is a mandatory parameter which specifies the file.
$offset: It is a mandatory parameter which specifies the new position of the
pointer. It is measured in bytes from the beginning of the file.
$whence: It is an optional parameter which can have the following possible
values-
SEEK_SET: It sets position equal to offset.
SEEK_CUR: It sets position to current location plus offset.
SEEK_END: It sets position to EOF plus offset. To move to a position before
EOF, the offset must be a negative value.
Example:
<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");
// reading first line
fgets($myfile);
// moving back to the beginning of the file
echo fseek($myfile, 0);
// closing the file
fclose($myfile);
?>
Output: 0
3.12 Copying Files With Copy
The copy() function in PHP is an inbuilt function which is used to make a
copy of a specified file.
It makes a copy of the source file to the destination file and if the
destination file already exists, it gets overwritten.
The copy() function returns true on success and false on failure.
Syntax:
copy(from_file, to_file, context)
Example:
<?php
echo
copy("test.txt","new.txt");
?>
Output:
1
<?php
// Copying gfg.txt to geeksforgeeks.txt
$srcfile = "test.txt";
$destfile = “copy.txt";
if (!copy($srcfile, $destfile))
{
echo "File cannot be copied! \n";
}
else
{
echo "File has been copied!";
}
?>
Output: File has been copied!
3.13 Deleting files
To delete a file by using PHP is very easy.
Deleting a file means completely erase a file from a directory so that
the file is no longer exist.
PHP has an unlink () function is used to delete a file.
Syntax
unlink( $filename, $context );
Example
<?php
$myFile = "test5.txt";
unlink($myFile) or die("Couldn't delete file");
?>
<?php
$file_pointer = fopen(“gfg.txt”, “w”);
fwrite($file_pointer, 'A computer science portal
for geeks!');
fclose($file_pointer);
if (!unlink($file_pointer))
{
echo ("$file_pointer cannot be deleted due to an
error");
}
else {
echo ("$file_pointer has been deleted");
} ?>
Output:
gfg.txt has been deleted
3.14 Reading and Writing Binary Files
With the basics of reading and writing text files complete, let's now turn our
attention to working with binary files.
Unlike text files, binary files can be much harder both to work with and
debug because they are by their very nature unreadable by anything but a
computer.
In PHP, writing binary files is done in the same manner as writing text files
(via the fputs() function) and therefore requires no explanation.
In fact, the only difference (which has already been mentioned) is the use of
the b mode when the file is opened via fopen().
Hence, this section will focus primarily on those functions relating to
reading binary data from a file and converting it into a form usable by PHP.
Specifically, we will be constructing a function that will read the header
from a Zip-compressed file and determine the minimum version number
required to decompress the data.
To accomplish this, we'll be examining the fseek(), fread(), and unpack()
functions.
Already fseek(), fread(), is given write that notes here.
Unpack() function:
The unpack() function is an inbuilt function in PHP which is used to unpack
from a binary string into the respective format.
Syntax:
unpack( $format, $data, $offset )
Example
<?php
var_dump ( unpack("C*", "GEEKSFORGEEKS"));
?>
Output:
array(13) { [1]=> int(71) [2]=> int(69) [3]=> int(69)
[4]=> int(75) [5]=> int(83) [6]=> int(70) [7]=>
int(79) [8]=> int(82) [9]=> int(71) [10]=> int(69)
[11]=> int(69) [12]=> int(75) [13]=> int(83) }
3.15 Locking files
The flock() function locks and releases a file.
Syntax
flock(file, lock, block)
Example
<?php
$file = fopen("test3.txt","w+");
if (f lock($file,LOCK_EX)) {
fwrite($file,"Add some text to the file.");
ff lush($file);
// release lock
echo “successfully done";
}
else
{
echo "Error locking file!";
}
fclose($file);
?>