Web Programming: Important Features
Web Programming: Important Features
Programming
Important Features
(Server Side Programming)
Important Features
• Form Handling
• Data Sanitization & Validation
• Server Side Includes
• File Upload
• Directory Functions
• File System Functions
• Cookies
• Sessions
• Hash Password Function
PHP
DIRECTORY FUNCTIONS
Directory Functions
The directory functions allow you to retrieve information about
directories and their contents.
• closedir ‐ Close directory handle
• opendir ‐ Open directory handle
• readdir ‐ Read entry from directory handle
NOTE: In computer programming, a handle is an abstract reference to a resource. Handles are used when
application software references blocks of memory or objects managed by another system, such as a database
or an operating system.
See: http://us2.php.net/manual/en/ref.dir.php
Open, Read & Close Directory
<?php
$somedirectory = "upload";
$dh = opendir($somedirectory);
closedir($dh);
?>
See: Example 20
PHP
FILE SYSTEM FUNCTIONS
File System Functions
The filesystem functions allow you to access and manipulate the
filesystem.
• is_file ‐ Tells whether the filename is a regular file
• is_dir ‐ Tells whether the filename is a directory
• fopen ‐ Opens file or URL
• fclose ‐ Closes an open file pointer
• feof ‐ Tests for end‐of‐file on a file pointer
• fgets ‐ Gets line from file pointer
• fgetc ‐ Gets character from file pointer
See: http://us2.php.net/manual/en/ref.filesystem.php
Is File
<?php
$somedirectory = "upload";
$dh = opendir($somedirectory);
closedir($dh);
?>
See: Example 21
Is File / Create Gallery
<?php
$somedirectory = "upload";
$dh = opendir($somedirectory);
closedir($dh);
?>
See: Example 22
Opening a File
• The fopen() function is used to open files in PHP.
<?php
$file=fopen("welcome.txt","r");
?>
• The first parameter of this function contains the name of the file to be
opened and the second parameter specifies in which mode the file should
be opened.
File Modes
• The file may be opened in one of the following modes:
• Note: If the fopen() function is unable to open the specified file, it returns 0 (false).
Reading a File Line by Line
• The fgets() function is used to read a single line from a file.
• Note: After a call to this function the file pointer has moved to the next line.
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open File!");
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
See: Example 23
Reading a File Character by Character
• The fgetc() function is used to read a single character from a file.
• Note: After a call to this function the file pointer has moved to the next character.
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open File!");
while(!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
See: Example 24
Write to File
• 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.
<?php
$file = fopen("myfile.txt", "w") or die("Unable to open file!");
fclose($file);
?>
See: Example 24 (Write to File)
Append to File
• If we want to add on to a file we need to open it up in append mode. The
code below does just that.
<?php
$file = fopen("myfile.txt", "a") or die("Unable to open file!");
fwrite($file, $txt);
fclose($file);
?>
See: Example 24 (Append to File)
Writing PHP File
<?php
$file = fopen("upload/myvariables.php", "w") or die("Unable to open file!");
$txt = '<?php'."\n";
fwrite($file, $txt);
$txt = '$x1="Value 1";'."\n";
fwrite($file, $txt);
$txt = '$x2="Value 2";'."\n";
fwrite($file, $txt);
$txt = '$x3="Value 3";'."\n";
fwrite($file, $txt);
$txt = '?>'."\n";
fwrite($file, $txt);
fclose($file);
See: Example 24 (Write PHP File)
Including PHP File
<?php include("upload/myvariables.php"); ?>
<!DOCTYPE html>
<html>
<head>
<title>PHP: Include Variables File</title>
<meta charset="utf-8" />
</head>
<body>
</body>
</html>
See: Example 24 Include PHP File)
More PHP Functions
file_put_contents()
• This function is identical to calling fopen(), fwrite() and fclose()
successively to write data to a file.
• If filename does not exist, the file is created. Otherwise, the existing file is
overwritten, unless the FILE_APPEND flag is set.
file_get_contents()
• Reads entire file into a string
• This function is similar to file(), except that file_get_contents() returns the
file in a string, starting at the specified offset up to maxlen bytes. On
failure, file_get_contents() will return FALSE.
See: http://php.net/manual/en/function.file‐put‐contents.php | http://php.net/manual/en/function.file‐get‐contents.php
More PHP Functions
<?php
$file = 'upload/welcome.txt’;
See: File Get Content / File Put Content
Important Features
• Form Handling
• Data Sanitization & Validation
• Server Side Includes
• File Upload
• Directory Functions
• File System Functions
• Cookies
• Sessions
• Hash Password Function