Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

PHP File and Directory Tutorial with Assignments

PHP File and Directory Tutorial with Assignments

This tutorial show you how to manipulate file and directory with PHP , we will learn how to create a directory, directory already existed or not, saving file in a directory you created and so on …

Directory:-

Creating a directory:
pro1.php
<?php
mkdir(“data”);
echo “Directory Created”;
?>
Question :- Write a program for create a directory . if directory available then give a message directory already exists, otherwise create. For checking the existence of directory is_dir() function is used.
pro2.php
<?php
if(is_dir(“data”))
echo “Directory already exists”;
else
{
mkdir(“data”);
echo “Directory created”;
}
?>
Question :-Write a program  to create a form with four field namely  NAME,ADDRESS,AGE,CONTACT.  Now a user will enter name, address, age, contact  then a file will create with name in data directory and all content will write in that file .
pro3.php
<?php
$nm=$_POST[‘t1’];
$add+$_POST[‘t2’];
$age=$_POST[‘t3’];
$cont=$_POST[‘t4’];
if(is_file($_POST[‘create’]))
{
if(is_file(“data/”.$nm.”.txt”))
echo “Echo file already exists”;
else
{
$fp=fopen(“data/”.$nm.”.txt”,’w’);
fwrite($fp,$nm.”\t”.$add.”\t”.$age.”\t”.$cont);
fclose($fp);
}
}
?>

<form name=”form1″ method=”post” action=””>
<table width=”227″ border=”1″ align=”center”>
<tr>
<td width=”56″>Name</td>
<td width=”155″><input type=”text” name=”t1″></td>
</tr>
<tr>
<td>Address</td>
<td><input type=”text” name=”t2″></td>
</tr>
<tr>
<td>Age</td>
<td><input type=”text” name=”t3″></td>
</tr>
<tr>
<td>Contact</td>
<td><input type=”text” name=”t4″></td>
</tr>
<tr>
<td><input name=”create” type=”submit” id=”create” value=”Create”></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

Assignment 1  : This program illustrate that how to read the content of particular directory(file name). For example we will read the content (file name ) of ‘data’ directory ,
[Note:-when you create a directory, two files automatic create that is (.) and(..) ]
pro4.php
<?php
$dp=opendir(“data”);
while($file=readdir($dp))
{
if($file!=’.’&&$file!=’..’)
$file_name[]=$file;
}
foreach($file_name as $f)
{
echo $f.”<br>”;
}
?>
Assignment 2  :  Modified Version of   Assignment 1 , try to understand it.
pro5.php
<?php
$fn=$_POST[‘t1’];
$msg=$_POST[‘msg’];
$task=$_POST[‘task’];
$select=$_POST[‘select’];
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’:
$fp=fopen(“data/”.$select,’r’);
$msg=fread($fp,filesize(“data/”.$select));

break;
case ‘append’:
$fp=fopen(“data/”.$select,’a’);
fwrite($fp,$msg);
break;

}
}

?>

<form action=”” method=”post”>
<table width=”405″ border=”1″ align=”center”>
<tr>
<td>File Name </td>
<td><input type=”text” name=”t1″></td>
</tr>
<tr>
<td width=”125″>Existing File </td>
<td width=”264″><label>
<select name=”select”>
<?php
$dp=opendir(“data”);
while($file=readdir($dp))
{
if($file!=’.’&&$file!=’..’)
$file_name[]=$file;
}
foreach($file_name as $f)
{
echo “<option value=’$f’>$f</option>”;
}
?>
</select>
</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>

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments