1. HEMCHANDRACHARYA NORTH GUJARAT UNIVERSITY, PATAN
B.C.A. Semester – VI
BCA-604 : Building Application Using PHP & ASP.NET
Teaching Scheme
(per week)
Examination Scheme
Teaching Scheme
(per semester)
INT
EXT
TOTAL
Th.
(hours)
Pr.
(hours)
Total
Hours
Credit
Th.
(marks)
Pr.
(marks)
Th.
(marks)
Pr.
(marks)
Th.
(marks)
Pr.
(marks)
--
4
40
4
--
30
--
70
--
100
University Examination Duration: 3 Hours (Per Batch)
(Practical List) : PHP (50%)
1. Write a PHP program to display “Hello World” Message on Screen.
Practical-1.php
<html>
<head>
<title>Practical-1</title>
</head>
<?php
echo "<b>Hello</b> <i>World</i>";
?>
<body>
</body>
</html>
Or
Practical-1a.php
<?php
echo "<b>Hello</b> <i>World</i>";
?>
Output:
Hello World
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 1
2. 2. Write a PHP program to display the today’s date and current time.
Practical-2.php
<?php
print strftime('%c');
echo "<br />";
print strftime('%d/%m/%Y');
echo "<br />";
print strftime('%A, %d %B - %Y');
echo "<br />";
echo "<b>Current Day, Date and Time is:</b> " . date("D M d, Y G:i A");
?>
Output:
01/19/14 11:31:16
19/01/2014
Sunday, 19 January - 2014
Current Day, Date and Time is: Sun Jan 19, 2014 11:31 AM
3. Write a PHP program to display the Fibonacci series.
Practical-3.php
<?php
$count = 0;
$no1 = 0;
$no2 = 1;
$tot = 0;
while($count <= 10)
{
echo $tot . "<br />";
$no1 = $no2;
$no2 = $tot;
$tot = $no1 + $no2;
$count ++;
}
?>
Or
Practical-3a.php
<?php
$number=10;
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 2
4. ?>
Output:
0
1
1
2
3
5
8
13
21
34
55
4. Write a PHP program to calculate sum of given number.
Practical-4.php
<?php
$val1=10;
$val2=10;
function sum($val1,$val2)
{
$total=$val1+$val2;
return $total;
}
echo "<b>Sum using Function : </b>" . sum($val1,$val2);
$sum=$val1 + $val2;
echo "<br />";
echo "<b>Sum is :</b> $sum";
?>
Output:
Sum using Function : 20
Sum is : 20
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 4
5. 5. Write a PHP Program that will use the concept form.
Practical-5.php
<html>
<head>
<title>Practical-5 Form Concept</title>
</head>
<body>
<form name="frmdemo" action="practical-5a.php" method="post" >
<fieldset>
<legend>Enter Your Details</legend>
<table width="250px" border="2" align="center">
<tr>
<td align="right">Name</td>
<td><input type="text" name="txtname"></td>
</tr>
<tr>
<td align="right">Contact No</td>
<td><input type="text" name="txtcno"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
Output:
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 5
23. 10. Write a PHP program to send Mail from PHP Script.
Practical-10.php
<?php
error_reporting(0);
$to = "me@localhost";
$subject = "This is subject Subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc@somedomain.com rn";
$header = "Cc:afgh@somedomain.com rn";
$header .= "MIME-Version: 1.0rn";
$header .= "Content-type: text/htmlrn";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
Output:
Message sent successfully...
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 23
24. 11. Write a PHP Program for Create, Delete, and Copying file from PHP Script.
practical-11a.php
// Enter File Name to Check Whether it is Exists or Not
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11a1.php" method="post" name="filehand">
Enter File Name to Check Whether it is Exists or Not.<br />
<input type="text" name="filename" /><br />
<input type="submit" name="checkfile" value="Check File" />
</form>
</body>
</html>
Output:
practical-11a1.php
<?php
//Get file name from user and store it to Variable
$filename=$_REQUEST['filename'];
//Check file exists or not.
if(isset($_REQUEST['checkfile']))
{
if (file_exists($filename))
{
echo "The file $filename exists". "<br>";
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 24
25. }
else
{
echo "The file $filename does not exists". "<br>";
}
}
?>
Output:
practical-11b.php
//Create Blank File
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11b1.php" method="post" name="filehand">
Enter File Name Here<br />
<input type="text" name="filename" /><br />
Click Here to Create Blank File<br />
<input type="submit" name="createfile" value="Create File" />
</body>
</html>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 25
26. Output:
practical-11b1.php
<?php
//Get file name from user and store it to Variable
$filename=$_REQUEST['filename'];
//Check file exists or not. if it is not exists then create new blank file
if(isset($_REQUEST['createfile']))
{
if (file_exists($filename))
{
echo "The file $filename exists". "<br>";
}
else
{
$handle = fopen($filename, 'w') or die('Cannot open file: '.$filename);
//Check Whether the file is created or nor.
if (file_exists($filename))
{
echo "The $filename file Successfully Created". "<br>";
}
else
{
echo "Error While Creating $filename". "<br>";
}
}
}
?>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 26
27. Output:
practical-11c.php
//Create, Open File and Save Data
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11c1.php" method="post" name="filehand">
Enter File Name Here<br />
<input type="text" name="filename" /><br />
Please Enter Text to save in selected file<br />
<textarea name="filedata" cols="20" rows="5"></textarea><br />
<input type="submit" name="savefile" value="Create/Save/Append File" />
</body>
</html>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 27
28. Output:
practical-11c1.php
<?php
//Get file name from user and store it to Variable
$filename=$_REQUEST['filename'];
//Check file exists or not. if exist it append the file. if not, then create new file and store data.
if(isset($_REQUEST['savefile']))
{
if (file_exists($filename))
{
$handle = fopen($filename, 'a') or die('Cannot open file: '.$filename);
$data = " " . $_REQUEST['filedata'];
fwrite($handle, $data . PHP_EOL);
echo "File Appended Successfully...";
}
else
{
$handle = fopen($filename, 'w') or die('Cannot open file: '.$filename);
$data = 'Welcome to Grow More Institute of Computer Application';
fwrite($handle, $data);
$data = " " . $_REQUEST['filedata'];
fwrite($handle, $data . PHP_EOL);
echo "File Successfully create a file and store Contents..";
}
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 28
29. }
?>
Output:
practical-11d.php
//Open File and Read Contents
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11d1.php" method="post" name="filehand">
Enter File Name Here<br />
<input type="text" name="filename" /><br />
Click here to Open File<br />
<input type="submit" name="openfile" value="Open/Read File" />
</body>
</html>
Output:
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 29
30. practical-11d1.php
<?php
//Get file name from user and store it to Variable
$filename=$_REQUEST['filename'];
//Check file exists or not. if exists then open the file and read the contents.
if(isset($_REQUEST['openfile']))
{
if (file_exists($filename))
{
$file = fopen($filename, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br>"; //if you want to read string contents.
//echo fgetc($file). "<br>"; //if you want to read on character at a time
}
fclose($file);
}
else
{
echo "File Does Not Exists..";
}
}
?>
Output:
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 30
31. practical-11e.php
//Open File and Read Contents
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11e1.php" method="post" name="filehand">
Enter File Name Here<br />
<input type="text" name="filename" /><br />
Click here to Open File<br />
<input type="submit" name="openfile" value="Open/Read File" />
</body>
</html>
Output:
practical-11e1.php
<?php
//Get file name from user and store it to Variable
$filename=$_REQUEST['filename'];
//Check file exists or not. if exists then open the file and read the contents.
if(isset($_REQUEST['openfile']))
{
if (file_exists($filename))
{
$filehand=fopen($filename,"r") or exit("Unable to open file!");;
$data=fread($filehand,filesize($filename));
echo "<br>" . $data;
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 31
32. fclose($filehand);
}
else
{
echo "File Does Not Exists..";
}
}
?>
Output:
practical-11f.php
// Copy File
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11f1.php" method="post" name="filehand">
Enter File Name Here<br />
<input type="text" name="filename" /><br />
Please enter new file name to copy file and press copy button<br />
<input type="text" name="newfilenm" /><br />
<input type="submit" name="copyfile" value="Copy File" />
</body>
</html>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 32
33. Output:
practical-11f1.php
<?php
//Get file name from user and store it to Variable
$filename=$_REQUEST['filename'];
$newfile=$_REQUEST['newfilenm'];
//Check file exists or not. if exists then copy the file.
if(isset($_REQUEST['copyfile']))
{
if (file_exists($filename))
{
if (file_exists($newfile))
{
echo "Destination file name is already exists...";
}
else
{
//copy file
copy($filename,$newfile);
echo "File Successfully copied...";
}
}
else
{
echo "File Does Not Exists..";
}
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 33
35. Output:
practical-11g1.php
<?php
//Check file exists or not. if exists then copy the file.
$delfile=$_REQUEST['deletefilenm'];
if(isset($_REQUEST['deletefile']))
{
if (file_exists($delfile))
{
//delete file
unlink($delfile);
if (file_exists($delfile))
{
echo "File Not Deleted";
}
else
{
echo "File Successfully Deleted...";
}
}
else
{
echo "File Does Not Exists..";
}
}
?>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 35
36. Output:
practical-11h.php
//File Information
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<form action="practical-11h1.php" method="post" name="filehand">
Please enter file name to get File Information.<br />
<input type="text" name="filenminfo" /><br />
<input type="submit" name="fileinfo" value="Get File Information" />
</body>
</html>
Output:
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 36
37. practical-11h1.php
<?php
//Check file exists or not. if exists then Display File Information.
$filename=$_REQUEST['filenminfo'];
if(isset($_REQUEST['fileinfo']))
{
if (file_exists($filename))
{
if (is_writable($filename))
{
echo "The file is writable". "<br>";
}
else
{
echo "The file is not writable". "<br>";
}
if (is_readable($filename))
{
echo "The file is readable". "<br>";
}
else
{
echo "The file is not readable". "<br>";
}
echo "$filename was last accessed: " . date("F d Y H:i:s.", fileatime($filename)) . "<br>";
echo "$filename was last changed: " . date("F d Y H:i:s.", filectime($filename)) . "<br>";
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename)) . "<br>";
echo "File Size ($filename):" . filesize($filename) . " bytes" . "<br>";
$path = dirname(__FILE__);
echo "Full path: " . $path . "<br>";
$path_parts = pathinfo($filename);
echo $path_parts['dirname'], "<br>";
echo $path_parts['basename'], "<br>";
echo $path_parts['extension'], "<br>";
echo $path_parts['filename'], "<br>";
echo realpath($filename) . "<br>";
}
else
{
echo "File Does Not Exists..";
}
}
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 37
38. ?>
Output:
practical-11i.php
// View Files with Links
<htm>
<head>
<title>File Handling</title>
</head>
<body>
<p>List of Files in Working Directory:</p>
<?php
$filelist = glob("*.txt");
foreach($filelist as $files)
{
echo "=> " . "<a href='$files'>$files</a>" . "<br>";
}
?>
</body>
</html>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 38
39. Output:
12. Write a PHP Program to Recursive Traversals of Directory.
Practical-12.php
<?php
/*
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
*/
header('Content-Type: text/plain');
$dir = dirname(__FILE__);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $item)
{
if ($item->isFile() || $item->isDir())
{
echo $item . PHP_EOL;
}
}
?>
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)
Page No. 39