D.N.R.
COLLEGE (AUTONOMOUS): BHIMAVARAM
DEPARTMENT OF COMPUTER SCIENCE
Web Applications Development using
PHP& MYSQL -7A
V SEMESTER
UNIT IV
WEB APPLICATIONS DEVELOPMENT USING PHP & MYSQL
UNIT IV
WORKING WITH FILES AND DIRECTORIES
Files: A File is a collection of data stored in the secondary memory. Files are used for storing information
that can be processed by the programs. Files are not only used for storing the data, programs are also stored
in files.
Directories: A directory is a unique type of file that contains only the information needed to access files or
other directories. A directory occupies less space than other types of files.
PHP INCLUDE : PHP allows you to include a file so that page content can be reused many times. It is very
helpful to include files when you want to apply the same HTML or PHP code to multiple pages of a website.
There are two ways to include file in PHP.
1. include
2. require
1. PHP include: PHP include is used to include a file on the basis of given path. You may use a relative or
absolute path of the file.
Syntax
There are two syntaxes available for include:
include 'filename ';
Or
include ('filename');
Example
//Save file as menu.html
<html>
<body>
<a href="http://www.javatpoint.com">Home</a> |
<a href="http://www.javatpoint.com/php-tutorial">PHP</a> |
<a href="http://www.javatpoint.com/java-tutorial">Java</a> |
<a href="http://www.javatpoint.com/html-tutorial">HTML</a>
</body>
</html>
//Save file as include1.php
<?php
Department of Computer Science Web Applications Development using PHP& MYSQL Page 1
include("menu.html");
?>
<h1>This is Main Page</h1>
Output:
PHP REQUIRE: PHP require is similar to include, which is also used to include files. The only difference is
that it stops the execution of script if the file is not found whereas includes doesn't.
Syntax
There are two syntaxes available for require:
require 'filename';
or
require ('filename');
Example
//Save file as menu2.html
<html>
<body>
<a href="http://www.javatpoint.com">Home</a> |
<a href="http://www.javatpoint.com/php-tutorial">PHP</a> |
Department of Computer Science Web Applications Development using PHP& MYSQL Page 2
<a href="http://www.javatpoint.com/java-tutorial">Java</a> |
<a href="http://www.javatpoint.com/html-tutorial">HTML</a>
</body>
</html>
//Save file as require1.php
<?php
require("menu.html");
?>
<h1>This is Main Page</h1>
Output:
PHP FILE HANDLING FUNCTIONS: PHP File System allows us to create file, read file , write file,
append file, delete file and close file.
1. 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).
Example
<?php
Department of Computer Science Web Applications Development using PHP& MYSQL Page 3
$file = fopen(“demo.txt”,'w');
?>
2. 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.
Syntax
int fwrite ( resource $handle , string $string [, int $length ] )
Example
<?php
$fp = fopen('data.txt', 'w');//open file in write mode
fwrite($fp, 'hello ');
fwrite($fp, 'php file');
fclose($fp);
echo "File written successfully";
?>
OUTPUT
File written successfully
3. PHP Read File - fread(): The PHP fread() function is used to read the content of the file. It accepts two
arguments: resource and file size.
Syntax
string fread ( resource $handle , int $length )
Example
<?php
$filename = "c:\\myfile.txt";
$handle = fopen($filename, "r");//open file in read mode
$contents = fread($handle, filesize($filename));//read file
echo $contents;//printing data of file
fclose($handle);//close file
?>
OUTPUT
hello php file
4. PHP Delete File - unlink(): The PHP unlink() function is used to delete file.
Syntax
bool unlink ( string $filename [, resource $context ] )
Department of Computer Science Web Applications Development using PHP& MYSQL Page 4
Example
<?php
unlink('data.txt');
echo "File deleted successfully";
?>
5. PHP Append Text: We can append data to a file by using the "a" mode. The "a" mode appends text to the
end of the file. In the example below we open our existing file "newfile.txt", and append some text to it.
Syntax:
resource fopen ( string filename, string mode )
Example
<?php
$fp = fopen('data.txt', 'a');//opens file in append mode
fwrite($fp, ' this is additional text ');
fwrite($fp, 'appending data');
fclose($fp);
echo "File appended successfully";
?>
OUTPUT:
File appended successfully
6. fclose() – file is closed using fclose() function. Its argument is file which needs to be closed.
Syntax
fclose(filepointer)
Example:
<?php
$file = fopen("demo.txt", 'r');
//some code to be executed
fclose($file);
echo “File closed successfully”
?>
OUTPUT:
File closed successfully
Department of Computer Science Web Applications Development using PHP& MYSQL Page 5
PHP DIRECTORIES: PHP directory functions as their name suggests are a set of functions used in
retrieving details, modifying them and fetching information on various file system directories and their
specific contents. A lot of operations can be performed on the directories like creating, deleting, changing the
present working directory, listing files present in the directory and so on. These basic functions are listed
below.
mkdir(): To make new directory.
opendir(): To open directory.
readdir(): To read from a directory after opening it.
closedir(): To close directory with resource-id returned while opening.
rmdir(): To remove directory.
1. Creating New Directory: For creating a new directory using PHP programming script, mkdir() function is
used and the usage of this function is as follows.
<?php
mkdir($directory_path, $mode, $recursive_flag, $context);
?>
This function accepts four arguments as specified. Among them, the first argument is mandatory,
whereas, the remaining set of arguments is optional.
$directory_path: By specifying either relative and absolute path, a new directory will be created in
such location if any, otherwise, will return an error indicating that there are no such locations.
$mode: The mode parameter accepts octal values on which the accessibility of the newly created
directory depends.
$recursive: This parameter is a flag and has values either true or false, that allow or refuse to create
nested directories further.
$context: As similar as we have with PHP unlink() having a stream for specifying protocols and etc.
2. Listing Directory Content in PHP: For listing the contents of a directory, we require two of the above
listed PHP directory functions, these are opendir() and readdir(). There are two steps in directory listing using
the PHP program.
Step 1: Opening the directory.
Step 2: Reading content to be listed one by one using a PHP loop.
Step 1: Opening Directory Link: opendir() function is used to perform this step. And, it has two arguments,
one is compulsory for specifying the path of the directory, and the other is optional, expecting stream context
if any. The syntax will be,
<?php
opendir($directory_path, $context);
Department of Computer Science Web Applications Development using PHP& MYSQL Page 6
?>
Step 2: Reading Directory Content: PHP readdir() will return string data on each iteration of the loop, and
this string will be the name of each item stored in the directory with its corresponding extension.
3. Closing Directory Link: Once the directory link is opened to perform a set of dependent operations like
reading directory content, we need to close this link after completing the related functionalities required. For
example,
<?php
$directory_handle = opendir($directory_path);
...
...
closedir($directory_handle);
?>
4. Removing Directory: PHP unlink() function is used to delete a file from a directory. Similarly, for
removing the entire directory, PHP provides a function named as rmdir() which accepts the same set of
arguments, as mkdir(). These are, the $directory_path and $context(Optional) as stated below.
<?php
rmdir($directory_path, $mode, $recursive_flag, $context);
?>
READING DATA FROM FILES: PHP provides various functions to read data from file. There are
different functions that allow you to read all file data, read data line by line and read data character by
character.
The available PHP file read functions are given below.
1. fread()
2. fgets()
3. fgetc()
1. PHP Read File - fread(): The PHP fread() function is used to read data of the file. It requires two
arguments: file resource and file size.
Syntax:
string fread (resource $handle , int $length )
$handle represents file pointer that is created by fopen() function.
$length represents length of byte to be read.
Example:
<?php
Department of Computer Science Web Applications Development using PHP& MYSQL Page 7
$filename = "c:\\file1.txt";
$fp = fopen($filename, "r");//open file in read mode
$contents = fread($fp, filesize($filename));//read file
echo "<pre>$contents</pre>";//printing data of file
fclose($fp);//close file
?>
OUTPUT:
this is first line
this is another line
this is third line
2. PHP Read File - fgets(): The PHP fgets() function is used to read single line from the file.
Syntax
string fgets ( resource $handle [, int $length ] )
Example
<?php
$fp = fopen("c:\\file1.txt", "r");//open file in read mode
echo fgets($fp);
fclose($fp);
?>
OUTPUT
this is first line
3. PHP Read File - fgetc(): The PHP fgetc() function is used to read single character from the file. To get all
data using fgetc() function, use !feof() function inside the while loop.
Syntax
string fgetc ( resource $handle )
Example
<?php
$fp = fopen("c:\\file1.txt", "r");//open file in read mode
while(!feof($fp))
{
echo fgetc($fp);
}
fclose($fp);
Department of Computer Science Web Applications Development using PHP& MYSQL Page 8
?>
OUTPUT
this is first line this is another line this is third line
WORKING WITH IMAGES
PHP IMAGECREATE( ) FUNCTION: Image create ( ) function is another inbuilt PHP function mainly
used to create a new image. The function returns the given image in a specific size. We need to define the
width and height of the required image. Instead of the image create ( ) function, we can also use other
creative functions like imagecreatetruecolor( ), which is a better alternative as it will return a better image
quality.
Syntax: In PHP, imagecreate( ) function follows the following syntax.
imagecreate( $width, $height )
S.No Parameter Description Optional /
mandatory
1 $ width This parameter is used to define the image's width that we Mandatory
want to display.
2 $ height This parameter is used to define the height of the image that Mandator
we want to display
The image creates ( ) function returns the resource identifier of an image on successful execution of the
program and FALSE on a failed attempt.
Example:
<?php
// to define the size of the image
$img = imagecreate(500, 300);
// to define the background color of the image
$bgcolor = imagecolorallocate($img, 150, 200, 180);
// to define the text color of the image
$fontcolor = imagecolorallocate($img, 120, 60, 200);
imagestring($img, 12, 150, 120, "Demo Text1", $fontcolor);
Department of Computer Science Web Applications Development using PHP& MYSQL Page 9
imagestring($img, 3, 150, 100, "Demo Text2", $fontcolor);
imagestring($img, 9, 150, 80, "Demo Text3", $fontcolor);
imagestring($img, 12, 150, 60, "Demo Text4", $fontcolor);
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
?>
OUTPUT:
Here in this program, we have declared various variables like $img to define the size of the image
that we require, $bgcolor to define the color of background we require, $fontcolor to define the color of text
we require. We have used the image string ( ) function to declare the string we want to display as an image.
To display the output of the image, we have used an inbuilt PHP command header and imagepng to display
on the browser.
WRITE A PHP PROGRAM TO DRAW DIFFERENT IMAGES.
<?php
header("Content-type: image/png");
$img_width = 800;
$img_height = 600;
$img = imagecreatetruecolor($img_width, $img_height);
Department of Computer Science Web Applications Development using PHP& MYSQL Page 10
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$orange = imagecolorallocate($img, 255, 200, 0);
imagefill($img, 0, 0, $white);
imagerectangle($img,$img_width*2/10, $img_height*5/10, $img_width*4/10, $img_height*8/10,
$red);
imagerectangle($img,$img_width*4/10, $img_height*5/10, $img_width*8/10, $img_height*8/10,
$red);
imagepolygon($img,[$img_width*3/10,$img_height*2/10,$img_width*2/10,$img_height*5/10,$img
_width*4/10, $img_height*5/10], 3, $red);
imageopenpolygon($img,[$img_width*3/10,$img_height*2/10,$img_width*7/10,$img_height*2/10,
$img_width*8/10, $img_height*5/10], 3, $red);
imageellipse($img, 100, 100, 100, 100, $orange);
imagearc($img, $img_width*3/10, $img_height*8/10, 100, 200, 180, 360, $red);
imageline($img, 0, $img_height*8/10, $img_width, $img_height*8/10, $green);
imagepng($img);
?>
OUTPUT
Department of Computer Science Web Applications Development using PHP& MYSQL Page 11
CREATE AN IMAGE USING PHP FUNCTIONS: PHP provides many functions to draw lines,
rectangles, polygons, arcs, ellipses, and much more. The GD(Graphics Draw) library is utilized for dynamic
picture creation. In PHP, we can easily use the GD library to make GIF, PNG, or JPG pictures quickly from
our code. So there is no need to write HTML and CSS code. We can easily handle this using the PHP
programming language. There are some PHP predefined functions used for generating images.
1. PHP imagecreatetruecolor() function: This function is used to create an image of the specified size. It
takes width and height as parameters.
Syntax:
imageceatetruecolor(int $width, int $height);
2. PHP imagecolorallocate() function: This function is used to fill the background colour of the image, and
it returns an identifier for that particular colour. It takes four parameters. Image resource in the first
parameter, and the other three parameters contain colour code in RGB component form.
Syntax:
imagecolorallocate(resource $image, int $red, int $green, int $blue);
3. PHP imagefilledrectangle() function: This function is used to draw a filled rectangle with the given start
and end coordinates. It takes six parameters. In the first parameter, it takes image resource and in the next
four parameters, it takes the rectangle coordinates and in the last parameter, it takes the color code to fill the
rectangle.
Syntax:
imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, $fillcolor );
4. PHP imagestring() function: The imagestring() function is used to write text to the image. In the first
parameter, it takes the image source, second parameter is the font size, which can be from 1 to 5 (smaller to
bigger), third and fourth parameters are the coordinates from the left corner and the fifth parameter is the
string to be written and the sixth parameter contains string color.
Syntax:
imagestring(resource $image, int $font , int $x , int $y , string $string , int $color );
5. PHP imagesetthickness() function: The imagesetthickness() function is used to set the thickness of line
drawing. In the first parameter, it takes an image resource, and in the second parameter it takes the thickness
size in pixels.
Syntax:
imagesetthickness(resource $image, int $size);
6. PHP imagepng() function: The imagepng() function is used to generate image in png format.
Syntax:
Department of Computer Science Web Applications Development using PHP& MYSQL Page 12
imagepng(resource $image);
7. PHP imagedestroy() function: The imagedestroy() function is used at last to free the allocated memory
associated with the image.
Syntax:
imagedestroy();
Department of Computer Science Web Applications Development using PHP& MYSQL Page 13