bca sem 6 php practicals 1to12

40
Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670) Page No. 1 HEMCHANDRACHARYA NORTH GUJARAT UNIVERSITY, PATAN B.C.A. Semester – VI BCA-604 : Building Application Using PHP & ASP.NET Teaching Scheme (per week) Teaching Scheme (per semester) Examination Scheme 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

Upload: hitesh-patel

Post on 25-Dec-2014

504 views

Category:

Education


30 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 1 

HEMCHANDRACHARYA NORTH GUJARAT UNIVERSITY, PATAN B.C.A. Semester – VI

BCA-604 : Building Application Using PHP & ASP.NET

Teaching Scheme (per week)

Teaching Scheme (per semester)

Examination Scheme

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

Page 2: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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;

Page 3: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 3 

$firstno=0; $secondno=1; $nextno=0; $cnt=0; echo "Fibonacci Series<br><br>"; for($cnt=0;$cnt<=$number;$cnt++) { if($cnt<=1) $nextno=$cnt; else { $nextno = $firstno + $secondno; $firstno = $secondno; $secondno = $nextno; } echo $nextno . "<br>"; } ?> or practical-3b.php <?php $number=10; $i=0; $cnt=0; echo "Fibonacci Series<br><br>"; for($cnt=0 ;$cnt<=$number;$cnt++) { echo fibonacci($i) . "<br>"; $i++; } function fibonacci($number) { if ( $number == 0 ) return 0; else if ( $number == 1 ) return 1; else return ( fibonacci($number-1) + fibonacci($number-2) ); }

Page 4: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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

Page 5: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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:

Page 6: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 6 

Practical-5a.php <?php if(isset($_REQUEST['submit'])) { $name=$_REQUEST['txtname']; $cno=$_REQUEST['txtcno']; echo "<b>Your Name is: </b>" . $name . "<br>"; echo "<b>Contact No: </b>" . $cno; } else { echo "Go Back and Press Submit button"; } ?>

Output:

Page 7: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 7 

6. Write a PHP program to read the employee detail using form component. Practical-6.php <html> <head> <title>Practical-6 Employee Form</title> </head> <body> <form name="empfrmdemo" action="practical-6a.php" method="post" > <fieldset> <legend>Grow More Computer Science</legend> <table width="360px" border="2" align="center"> <caption><b>Enter Employee Detail...</b></caption> <tr> <td align="right">Employee Name</td> <td><input type="text" name="empname" size="30px"></td> </tr> <tr> <td align="right">Address</td> <td><textarea name="empadd" cols="23"></textarea></td> </tr> <tr> <td align="right">Department</td> <td><select name="empdept"> <option value="BCA" selected>BCA</option> <option value="PGDCA">PGDCA</option> <option value="M.Sc.(CA&IT)">MSC(CA/IT)</option> </select> </td> </tr> <tr> <td align="right">Designation</td> <td><select name="empdes"> <option selected="" value="Principal">Principal</option> <option value="Professor">Professor</option> <option value="Asst.Professor">Asst.Professor</option> <option value="Lecturer">Lecturer</option> <option value="Clerk">Clerk</option> </select> </td> </tr> <tr> <td align="right">Male/Female</td> <td>

<input type="radio" name="gender" value="male" checked="checked">Male<br>

Page 8: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 8 

<input type="radio" name="gender" value="female">Female </td> </tr> <tr> <td align="right">Favourite Subjects</td> <td> <input type="checkbox" name="favsub[]" value="Hacking" checked="checked" />Hacking<br /> <input type="checkbox" name="favsub[]" value="Photoshop" />Photoshop<br /> <input type="checkbox" name="favsub[]" value="Forensic" checked="checked"/>Forensic<br /> <input type="checkbox" name="favsub[]" value="C Language" />C Language<br /> <input type="checkbox" name="favsub[]" value="PHP" checked="checked"/>PHP </td> </tr> <tr> <td align="right">Contact No</td> <td><input type="text" name="empcno" size="30px"></td> </tr> <tr> <td align="right">E-Mail</td> <td><input type="text" name="empmail" size="30px"></td> </tr> <tr> <td align="right">Password</td> <td><input type="password" name="emppass" size="30px"></td> </tr> <tr> <td align="right">Upload Photo</td> <td><input type="file" name="empphoto" accept="image/*" size="30px"></td> </tr> <tr> <td colspan="2" align="center"> <input type="hidden" name="empid" value="101"> <input type="submit" name="submit" value="Submit"> </td> </tr> </table> </fieldset> </form> </body> </html>

Page 9: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 9 

Output:

Page 10: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 10 

Page 11: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 11 

Page 12: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 12 

7. Write a PHP program to demonstrate the use of array. Practical-7.php <?php /*Indexed Array*/ // Indexed Array using Method-1 $gm=array("BCA","PGDCA","MSCIT"); echo "I like " . $gm[0] . ", " . $gm[1] . " and " . $gm[2] . "."; echo "<br>"; //Indexed Array using Method-2 $gm[0]="BCA"; $gm[1]="PGDCA"; $gm[2]="MSCIT"; echo "I like " . $gm[0] . ", " . $gm[1] . " and " . $gm[2] . "."; echo "<br>Elements in Array:"; $arrsize=count($gm); for($cnt=0;$cnt<$arrsize;$cnt++) { echo "<br>"; echo "<b>" . $gm[$cnt] . "</b>"; } echo "<br>"; /*Associative Array*/ // Associative Array using Method-1 $gm=array("d1"=>"BCA","d2"=>"PGDCA","d3"=>"MSCIT"); echo "Degree is " . $gm['d1'] ; echo "<br>"; //Associative Array using Method-2 $gm['d1']="BCA"; $gm['d2']="PGDCA"; $gm['d3']="MSCIT"; echo "Degree is " . $gm['d2'] ; echo "<br>"; foreach($gm as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>

Page 13: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 13 

Output: I like BCA, PGDCA and MSCIT. I like BCA, PGDCA and MSCIT. Elements in Array: BCA PGDCA MSCIT Degree is BCA Degree is PGDCA Key=d1, Value=BCA Key=d2, Value=PGDCA Key=d3, Value=MSCIT 8. Write a PHP program to prepare student Mark sheet using Switch statement. Practical-8.php <html> <head> <title>Practical-8 Marksheet</title> </head> <body> <form name="frmdemo" action="practical-8a.php" method="post" > <fieldset> <legend align="center">Enter Your Name with Marks Detail</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">Subject-1</td> <td><input type="text" name="txtsub1"></td> </tr> <tr> <td align="right">Subject-2</td> <td><input type="text" name="txtsub2"></td> </tr> <tr> <td align="right">Subject-3</td> <td><input type="text" name="txtsub3"></td> </tr> <tr> <td align="right">Subject-4</td> <td><input type="text" name="txtsub4"></td> </tr>

Page 14: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 14 

<tr> <td colspan="2" align="center">

<input type="submit" name="submit" value="Submit"></td> </tr> </table> </fieldset> </form> </body> </html>

Output:

Page 15: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 15 

Practical-8a.php <?php if(isset($_REQUEST['submit'])) { $name=$_REQUEST['txtname']; $sub1=$_REQUEST['txtsub1']; $sub2=$_REQUEST['txtsub2']; $sub3=$_REQUEST['txtsub3']; $sub4=$_REQUEST['txtsub4']; echo "<b>Your Name is: </b>" . $name . "<br>"; echo "<b>Your Marks Detail: </b> <br>"; echo "Subject-1: " . $sub1 . "<br>"; echo "Subject-2: " . $sub2 . "<br>"; echo "Subject-3: " . $sub3 . "<br>"; echo "Subject-4: " . $sub4 . "<br>"; $total=$sub1+$sub2+$sub3+$sub4; echo "Total Marks: " . $total . "<br>"; $per=$total/4; echo "Percentage: " . $per . "%<br>"; switch($per) { case $per<35: echo "Grade: F" . "<br>"; break; case $per>=35 && $per<=50: echo "Grade: D" . "<br>"; break; case $per>50 && $per<=60: echo "Grade: C" . "<br>"; break; case $per>60 && $per<=70: echo "Grade: B" . "<br>"; break; case $per>70 && $per<100: echo "Grade: A" . "<br>"; break; default: echo "Invalid.... or out of limit"; break; } }

Page 16: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 16 

else { echo "Go Back and Press Submit button"; } ?> Output: Your Name is: Hitesh Patel Your Marks Detail: Subject-1: 40 Subject-2: 40 Subject-3: 40 Subject-4: 40 Total Marks: 160 Percentage: 40% Grade: D 9. Write a PHP program to generate the multiplication of matrix. Practical-9.php <?php $p = array(); $p[] = array(1,3,-4); $p[] = array(1,1,-2); $p[] = array(-1,-2,5); $q = array(); $q[] = array(8,3,0); $q[] = array(3,10,2); $q[] = array(0,2,6); echo "matrix 1<br/>"; echoMatrix(3, $p); echo "<br/>"; echo "matrix 2<br/>"; echoMatrix(3, $q); echo "<br/>"; $r = matrixMultiply(3, $p, $q); echo "result of matrix multiply<br/>"; echoMatrix(3, $r); function echoMatrix($N, $r) {

Page 17: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 17 

for($i=0; $i<$N; $i++) { for($j=0; $j<=$N; $j++) { if ($j==$N) { echo "<br>"; } else { echo $r[$i][$j]; } if ($j<($N-1)) { echo ", "; } } } } function matrixMultiply($N, $p, $q) { //init result $r = array(); for($i=0; $i<$N; $i++) { $t = array(); for($j=0; $j<$N; $j++) { $t[] = 0; } $r[] = $t; } //do the matrix multiply for($i=0; $i<$N; $i++) { for($j=0; $j<$N; $j++) { $t = 0; for($k=0; $k<$N; $k++) { $t += $p[$i][$k] * $q[$k][$j]; } $r[$i][$j] = $t; } }

Page 18: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 18 

//return result return $r; } ?>

Output:

Practical-9a.php <html> <head> <title>Matrix Multiplication</title> </head> <body> <form name="matrix" action="practical-9b.php" method="get"> <table border="2" cellpadding="2" cellspacing="3"> <caption><b>Please Enter 3x3 Matrix</b></caption> <tr> <td rowspan="3">Matrix-A</td> <td> <input name="a11" type="text" style="width: 50px"></td>

Page 19: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 19 

<td> <input name="a12" type="text" style="width: 50px"></td> <td> <input name="a13" type="text" style="width: 50px"></td> </tr> <tr> <td> <input name="a21" type="text" style="width: 50px"></td> <td> <input name="a22" type="text" style="width: 50px"></td> <td> <input name="a23" type="text" style="width: 50px"></td> </tr> <tr> <td> <input name="a31" type="text" style="width: 50px"></td> <td> <input name="a32" type="text" style="width: 50px"></td> <td> <input name="a33" type="text" style="width: 50px"></td> </tr> <tr> <td rowspan="3">Matrix-B</td> <td> <input name="b11" type="text" style="width: 50px"></td> <td> <input name="b12" type="text" style="width: 50px"></td> <td> <input name="b13" type="text" style="width: 50px"></td> </tr> <tr> <td> <input name="b21" type="text" style="width: 50px"></td> <td> <input name="b22" type="text" style="width: 50px"></td> <td> <input name="b23" type="text" style="width: 50px"></td> </tr> <tr> <td> <input name="b31" type="text" style="width: 50px"></td> <td> <input name="b32" type="text" style="width: 50px"></td> <td> <input name="b33" type="text" style="width: 50px"></td> </tr> <tr>

Page 20: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 20 

<td align="center" colspan="4"> <input name="Submit" type="submit" value="submit"></td> </tr> </table> </form> </body> </html>

Output

Practical-9b.php <?php if(isset($_REQUEST['submit'])) { $a = array(); $a[] = array($_REQUEST['a11'],$_REQUEST['a12'],$_REQUEST['a13']); $a[] = array($_REQUEST['a21'],$_REQUEST['a22'],$_REQUEST['a23']); $a[] = array($_REQUEST['a31'],$_REQUEST['a32'],$_REQUEST['a33']); $b = array(); $b[] = array($_REQUEST['b11'],$_REQUEST['b12'],$_REQUEST['b13']); $b[] = array($_REQUEST['b21'],$_REQUEST['b22'],$_REQUEST['b23']); $b[] = array($_REQUEST['b31'],$_REQUEST['b32'],$_REQUEST['b33']);

Page 21: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 21 

echo "Matrix-A<br/>"; dispmatrix(3, $a); echo "<br/>"; echo "Matrix-B<br/>"; dispmatrix(3, $b); echo "<br/>"; $r = matrixMultiply(3, $a, $b); echo "Matrix Multiplication<br/>"; dispmatrix(3, $r); } else { header('location:practical-9a.php'); } function dispmatrix($N, $r) { for($i=0; $i<$N; $i++) { for($j=0; $j<=$N; $j++) { if ($j==$N) { echo "<br>"; } else { echo $r[$i][$j]; } if ($j<($N-1)) { echo ", "; } } } } function matrixMultiply($N, $a, $b) { //init result $r = array(); for($i=0; $i<$N; $i++) { $t = array(); for($j=0; $j<$N; $j++) { $t[] = 0;

Page 22: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 22 

} $r[] = $t; } //do the matrix multiply for($i=0; $i<$N; $i++) { for($j=0; $j<$N; $j++) { $t = 0; for($k=0; $k<$N; $k++) { $t += $a[$i][$k] * $b[$k][$j]; } $r[$i][$j] = $t; } } //return result return $r; } ?>

Output:

 

Page 23: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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:[email protected] \r\n"; $header = "Cc:[email protected] \r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-type: text/html\r\n"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; } else { echo "Message could not be sent..."; } ?> Output: Message sent successfully...

Page 24: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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>";

Page 25: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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>

Page 26: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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>"; } } } ?>

Page 27: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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>

Page 28: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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.."; }

Page 29: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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:

Page 30: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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:

Page 31: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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;

Page 32: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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>

Page 33: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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.."; }

Page 34: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 34 

} ?>

Output:

practical-11g.php //Delete File <htm> <head> <title>File Handling</title> </head> <body> <form action="practical-11g1.php" method="post" name="filehand"> Please enter file name to delete file.<br /> <input type="text" name="deletefilenm" /><br /> <input type="submit" name="deletefile" value="Delete File" /> </body> </html>

Page 35: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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.."; } } ?>

Page 36: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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:

Page 37: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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.."; } }

Page 38: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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>

Page 39: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 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; } } ?>

Page 40: Bca sem 6 php practicals 1to12

Created By: Hitesh Patel (Asst. Professor, Grow More BCA – 9998531670)  Page No. 40 

Output: