PHP file handling assignments with complete solution

PHP file handling assignments with complete solution

PHP file handling Assignment 1

Question:- create a form of  four field as name, address, age and password. Input name, address, age and password through form. Then create a file with name and write name, address, age, password in that file. is_file() function  check the existence of file in “data” directory.
[Note:- create a “data”directory in your project directory]
Solution:-
pro1.php
<?php
$nm=$_POST[‘t1’];
$add=$_POST[‘t2’];
$age=$_POST[‘t3’];
if(isset($_POST[‘insert’]))
{
if(is_file(“data/”.$nm.”.txt”))
{
echo “File already exist”;
}
else
{
$fp=fopen(“data/”.$nm.”.txt”,’w’);
fwrite($fp,$nm.”\t”.$add.”\t”.$age);
fclose($fp);
}
}
?>
<form  method=”post”  action=””>
<table width=”235″ border=”1″ align=”center”>
<tr>
<td width=”56″>Name</td>
<td width=”163″><label>
<input type=”text” name=”t1″>
</label></td>
</tr>
<tr>
<td>Address</td>
<td><label>
<input type=”text” name=”t2″>
</label></td>
</tr>
<tr>
<td>Age</td>
<td><label>
<input type=”text” name=”t3″>
</label></td>
</tr>
<tr>
<td><input name=”insert” type=”submit” id=”insert” value=”Insert” />
</td>
<td>&nbsp;</td>
</tr>
</table>
</form>

PHP file handling Assignment 2

Question –  In this example a form have three input field namely File Name(text field), Message(text area),Task(list box) and a Done (submit button). A user will enter a file name and enter some text in Message (text area) then select Create Task from (list box) and submit the Done button. If file not available in data(directory) then a file will create with file name and content of Message will write in the file. if file not available in the data directory then a message will appear in Message(text area) that “ File Doesn’t exists .”. when user enter a file name  and select Read from list box , if file is available then  content of existing file will appear in message box. Again user will enter file name and select Append from list box then file will appended with content.
In this example file_exists() function is used for checking the file existence in “data” directory.
[Note:- create a “data” directory in your project directory]
pro2.php
<?php
$fn=$_POST[‘t1’];
$msg=$_POST[‘msg’];
$task=$_POST[‘task’];
if(isset($_POST[‘done’]))
{
switch($task)
{
case ‘write’:
if(file_exists(“data/”.$fn.”.txt”))
$msg=”File name already exist”;
else
{
$fp=fopen(“data/”.$fn.”.txt”,’w’);
fwrite($fp,$msg);
fclose($fp);
}
break;
case’read’:
if(file_exists(“data/”.$fn.”.txt”))
{
$fp=fopen(“data/”.$fn.”.txt”,’r’);
$msg=fread($fp,filesize(“data/”.$fn.”.txt”));
}
else
$msg=”File does’t Exists”;
break;
case ‘append’:
if(file_exists(“data/”.$fn.”.txt”))
{
$fp=fopen(“data/”.$fn.”.txt”,’a’);
fwrite($fp,$msg);
}
else
$msg=”File does’t Exists”;
break;
}
}

?>
<form action=”” method=”post”>
<table width=”405″ border=”1″ align=”center”>
<tr>
<td width=”125″>File Name </td>
<td width=”264″><label>
<input type=”text” name=”t1″>
</label></td>
</tr>
<tr>
<td height=”292″>Massage</td>
<td><label>
<textarea name=”msg” rows=”12″ cols=”35″><?php echo $msg;?></textarea>
</label></td>
</tr>
<tr>
<td>Task
<label></label></td>
<td><select name=”task”>
<option value=”write”>Craete</option>
<option value=”read”>Read</option>
<option value=”append”>Append</option>
</select></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type=”submit” name=”done” value=”DONE”>
</label></td>
</tr>
</table>
</form>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion