PHP PROGRAMS || FILES

PHP PROGRAMS




PHP PROGRAMS || FILES





Q1) WRITE A PHP PROGRAM TO READ A DIRECTORY NAME AND EXTENSION FROM USER DISPLAY THE FILES WITH SPECIFIED EXTENSION FROM THAT DIRECTORY.


-->


<html>

<body>
<form action="directoryextension.php" method="GET">
Enter Directory name:<input type="text" name="directory"><br>
<br>Enter Extension with dot(.):<input type="text" name="extension"><br>
<br><input type="submit">
</form>
</body>
</html>


<?php

$directory=$_GET['directory'];
$extension=$_GET['extension'];
if(!is_dir($directory))
{

die("Invalid Directory Name: $directory");
}
$handle=opendir($directory);
echo "<br>Displaying files with extension: $extension";
$cnt=1;
while(($filename=readdir($handle))!==false)
{
if(strstr($filename,$extension))
{
echo "<br>$cnt:$filename";
$cnt++;
}
}
closedir($handle);
?>


OUTPUT -



Enter Directory name:

Enter Extension with dot(.):





Q2)  WRITE A PHP PROGRAM TO READ FILE "STUDENT.DAT" THAT HAS STUDENT DETAILS AS ROLLNO, NAME, M1,M2,M3 AS MARKS OF 3 SUBJECTS. DISPLAY THE DATA FROM THE FILE IN TABULAR FORMAT. ALSO DISPLAY TOTAL AND PERCENTAGE OF EACH STUDENT.


-->

STUDENT.DAT FILE

1 abhilash 60 78 89

2 shubham 70 89 87
3 sanket 89 96 92


<?php

$fp=fopen("student.dat","r");
echo "<h2>STUDENT INFORMATION</h2><br>";
echo "<table border=1>";
echo "<tr>";
echo "<td><b>Rollno</b></td><td><b>Name</b></td><td><b>Mark1</b></td><td><b>Mark2</b></td><td><b>Mark3</b></td><td><b>Total</b></td><td><b>Percentage</b></td>";
echo "</tr>";
while($data=fscanf($fp,"%d\t%s\t%d\t%d\t%d\n"))
{
list($rno,$name,$m1,$m2,$m3)=$data;
$total=($m1+$m2+$m3);
$per=$total/3;
echo "<tr>";
echo "<td>$rno</td><td>$name</td><td>$m1</td><td>$m2</td><td>$m3</td><td>$total</td><td>$per</td>";
echo "</tr>";
}
echo "</table>";
?>


Q3) WRITE A PHP PROGRAM TO READ TWO FILE NAMES FROM USER AND APPEND CONTENT OF FIRST FILE INTO SECOND FILE.

-->

<html>

<body>
<form action="appendfile.php" method="POST">
Enter First File:<input type="text" name="first"><br><br>
Enter Second File:<input type="text" name="second"><br><br>
<input type="submit">
</form>
</body>
</html>


<?php
$first=$_POST['first'];
$second=$_POST['second'];
if(!file_exists($first))
{
die("<br>$first does not exist");
}
if(!file_exists($second))
{
die("<br>$second does not exist");
}
$fp1=fopen($first,'r') or die("unable to open first file");
$fp2=fopen($second,'a') or die("unable to open second file");
$data=fread($fp1,filesize($first));
fwrite($fp2,$data);
fclose($fp1);
fclose($fp2);
echo "<h2>Contents of first file is appanded to second file</h2>";
?>

OUTPUT -

Enter First File:

Enter Second File:




Q4) FILE OPEN PROGRAM


-->


<?php

$myfile = fopen("FILE.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("FILE.txt"));
fclose($myfile);
?>

Q5) FILE WRITE PROGRAM


-->


<?php

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "SALMAN\n";
fwrite($myfile, $txt);
$txt = "SALMAN\n";
fwrite($myfile, $txt);
fclose($myfile);
?>


OUTPUT -

SALMAN 
SALMAN

Q6) WRITE A PROGRAM TO OVERRIDE A FILE.

-->

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "SALMAN KHAN\n";
fwrite($myfile, $txt);
$txt = "SALMAN NAIK\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

OUTPUT -

SALMAN KHAN 
SALMAN NAIK


Q7)WRITE A MENU DRIVEN PROGRAM TO PERFORM VARIOUS FILE OPERATION. ACCEPT FILOE NAME FROM USER.

a)DISPLAY TYPE OF FILE. 
b)DELETE A FILE.

-->

<html>
<body>
<form action="fileoperations.php" method="POST">
Enter FileName:<input type="text" name="name"><br><br>
<input type="radio" value="1" name="option">Display the type of the file<br><br>
<input type="radio" value="2" name="option">Delete the file<br><br>
<input type="submit">
</form>
</body>

</html>


<?php
$fname=$_POST['name'];
if(!(isset($fname)))
{
die("Please enter a file name first.....");
}
if(!(is_file($fname)))
{
die("Given filename is not a file");
}
if(!(file_exists($fname)))
        {
                die("Given Filename does not exists");
        }
$choice=$_POST['option'];
switch($choice)
{
case 1: $ext=end(explode('.',$fname));
echo "It is an <b>$ext</b> file";
break;
case 2: unlink($fname);
echo "File <b>$fname</b> is deleted successfully";
break;
default : echo "Please choose an option...";
}
?>



Post a Comment

1 Comments

  1. That method, not a turn will go by the place you see a winner however don't get it reflected in your winnings (because you didn't bet the line). The machine doesn't care which strains you're betting and will not regulate its payout charges accordingly, so bet 'em all to catch 'em all. In other phrases, 카지노사이트 this machine doesn't enhance the amount you win if were to bet more, it just will increase the amount of winning combos or places you have have} access to. And no, if the machine hits however you didn't pay for it, the machine nonetheless counts that as a payout -- it will not make up for it in its 93% payback .

    ReplyDelete