0% found this document useful (0 votes)
14 views

Web Programming: Important Features

Web programming involves important server-side features like form handling, data validation, file uploads, and directory and file system functions. Directory functions allow retrieving information about directories and their contents using functions like opendir(), readdir(), and closedir(). File system functions provide access and manipulation of files, with functions like is_file(), fopen(), fgets(), and fclose() to open, read, and close files. Functions like file_put_contents() and file_get_contents() allow writing to and reading from files.

Uploaded by

M Fayez Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Web Programming: Important Features

Web programming involves important server-side features like form handling, data validation, file uploads, and directory and file system functions. Directory functions allow retrieving information about directories and their contents using functions like opendir(), readdir(), and closedir(). File system functions provide access and manipulation of files, with functions like is_file(), fopen(), fgets(), and fclose() to open, read, and close files. Functions like file_put_contents() and file_get_contents() allow writing to and reading from files.

Uploaded by

M Fayez Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Web 

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);

while (false !== ($afilename = readdir($dh))) {


print("$afilename<br>\n");
}

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);

while (false !== ($afilename = readdir($dh))) {


if (is_file($somedirectory."/".$afilename))
print("$afilename<br>\n");
}

closedir($dh);
?>

See: Example 21
Is File / Create Gallery
<?php
$somedirectory = "upload";
$dh = opendir($somedirectory);

while (false !== ($afilename = readdir($dh))) {


if (is_file($somedirectory."/".$afilename))
{
$rp=$somedirectory."/".$afilename;
print("<a href=\"$rp\">");
print("<img src=\"$rp\" width=\"200\" />");
print("</a>\n");
}
}

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!");

$txt = "John Doe\n";


fwrite($file, $txt);

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!");

$txt = "Something New\n";

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);

echo "File Created Successfully!";


?>

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>

<h1>Welcome to my home page!</h1>


<p>Value of x1=<?php echo $x1; ?></p>
<p>Value of x2=<?php echo $x2; ?></p>
<p>Value of x3=<?php echo $x3; ?></p>

</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’;

// Open the file to get existing content


$current = file_get_contents($file);

// Append a new person to the file


$current .= "John Smith\n";

// Write the contents back to the file


file_put_contents($file, $current, FILE_APPEND | LOCK_EX);
?>

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

You might also like