File Handling – PHP

Note: You can Download all .PHP Files directly here.

You can also find other Programs in About Page.

kg.php

<html>
<head><title>Kanulp’s Blog</title>
<style type=”text/css”>
#search
{
width: 100px;
height: 30px;
color:white;
background-color:blue;
}
#search:hover
{
width: 110px;
height: 30px;
color:black;
background-color:yellow;
}
.required{
color:red;
}
</style>
</head>
<body>
<?php
$error=0;
$success=””;
$nameq = $rateq = $yearq = $monthq = $dayq = $emailq=””;

if($_SERVER[“REQUEST_METHOD”]==”POST”){

if (ctype_alpha(str_replace(‘ ‘, ”, $_POST[“name”])) === false) {
$nameq=”Name Should consist only letters”;
$error=1;
}
else if (empty($_POST[“name”])){
$nameq=”Name is required”;
$error=1;
break;
}
else{
$error=0;
}

if(!isset ( $_POST[“trust”] ) ) {
$rateq = “For your safety rate this member!!!”;
$error=1;
}

if ( ! filter_var ( $_POST[“email”] , FILTER_VALIDATE_EMAIL ) ) {

$emailq = “Email is not valid”;
$error=1;
}

if(empty($_POST[“year”]))
{
$yearq=”Year is required for bday!”;
$error=1;
}

if(empty($_POST[“month”]))
{
$monthq=”Month is required”;
$error=1;
}

if(empty($_POST[“day”]))
{
$dayq=”Day is required!”;
$error=1;
}

if($error == 0)
{
$success = “Submitted successfully…”;

$d2 = $_POST[“year”];
$d1 = date(‘Y’);
$dif = $d1 – $d2;
$t = $_POST[“trust”];

switch($t)
{
case 1:$trate=”Trustable”;
break;

case “2”:$trate=”Beware to work with”;
break;

case “3”:$trate=”Think 100 times before work with”;
break;
}

$output = “Name:”.$_POST[“name”].”,”.”\tAge:”.$dif.”,”.”\tMastery:”.$_POST[“interest”].”,”.”\tEmail:”.$_POST[“email”].”,”.”IMP:”.$trate .”\n”;

$fp = fopen(“C:\wamp\www\k.csv”,”a”);
fwrite($fp,$output);
fclose($fp);
}
} ?>
<h2>Your Programing Team Details:</h2>
<form action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>” method=”post” enctype=”multipart/form-data”>
<table>

<tr><td><lable>Name:</lable></td>
<td><input type=”text” name=”name”>

</td>
</tr>

<tr>
<td><lable>D.O.B:</lable></td>

<td><select name=”year”>
<option value=””>Year</option>
<?php for ($year = date(‘Y’); $year > date(‘Y’)-100; $year–) { ?>
<option value=”<?php echo $year; ?>”> <?php echo $year; ?></option>
<?php } ?>
</select>

<select name=”month”>
<option value=””>Month</option>
<?php for ($month = 1; $month <= 12; $month++) { ?>
<option value=”<?php echo strlen($month)==1 ? ‘0’.$month : $month; ?>”><?php echo strlen($month)==1 ? ‘0’.$month : $month; ?></option>
<?php } ?>
</select>

<select name=”day”>
<option value=””>Day</option>
<?php for ($day = 1; $day <= 31; $day++) { ?>
<option value=”<?php echo strlen($day)==1 ? ‘0’.$day : $day; ?>”><?php echo strlen($day)==1 ? ‘0’.$day : $day; ?></option>
<?php } ?>
</select><span class=”required”><?PHP echo $yearq.” “.$monthq.” “.$dayq; ?></span></td>

</tr>
<tr>
<td>Mastery:</td>
<td><select name=”interest”>
<option value=”php”>PHP</option>
<option value=”android”>Android</option>
<option value=”c++”>C++</option>
<option value=”java”>JAVA</option>
<option value=”html”>HTML & CSS</option>
</select></td></tr>

<tr>
<td>Email:</td>
<td><input type=”text” name=”email”><span class=”required”><?php echo $emailq ?></span></td>
</tr>

<tr>
<td>Trustable Rate:</td>
<td>
<input type=”radio” name=”trust” value=”1″>1
<input type=”radio” name=”trust” value=”2″>2
<input type=”radio” name=”trust” value=”3″>3
<span class=”required”><?php echo $rateq ?></span></td>
</tr>
</table>
<pre>
<input type=”submit” id=”search” name=”submit” value=”Done”></pre>

</form>

<?php

?>

</body>
</html>

So, I have used some CSS styles for hover and validation in for filing up.

Then I have set some validations explain in code comments.They will be executed when the submit button is clicked.

There is and variable called $output which basically contains strings what we have to store in our file.

File Handling functions.

To open a file in various modes: fopen();

in this function there are two parameter first we have to give path and second how we want to open file like in read/write/append mode ex. fopen(“C:\wamp\www\k.csv”,”a”);

I have used .CSV file. You can open .txt also. Now to write in files there is a function fwrite();

two parameters are 1.file path and 2.What to write in file.

Then close the file with fclose(filename);


Read From file.

readteams.php

<html>
<head><title>Your teams</title>
</head>
<body>
<h1>Teams</h1>

<?php

$fp = fopen(“C:\\wamp\\www\\k.csv”,”r”); #to open in windows

while(!feof($fp)){

$members = fgets($fp);
echo $members.”</br>”;

}
fclose($fp);
?>
</body>
</html>

To read from file we have to first open file in read mode

then with the help of feof(); and fgets(); we have read all lines.

The fgets() function stops at  EOF or we can set length like 110 to stop.


If you still have doubt can contact.

Example 1 – PHP

Simple Calc In PHP

<html>
<head><title>Calc</title></head>
<body>
<h3>Simple Calculator in PHP</h3>
<form action =”#” method=”get”>

<lable>Enter Num 1:</lable>
<input type=”number” name=”num1″ required=”required”><br>
<lable>Enter Num 2:</lable>
<input type=”number” name=”num2″ required=”required”><br>

<table>
<tr>
<td>Add:</td>
<td><input type=”checkbox” name=”check[]” value=”add”></td>
<td>Subtract:</td>
<td><input type=”checkbox” name=”check[]” value=”subtract”></td>
<td>Multiply:</td>
<td><input type=”checkbox” name=”check[]” value=”multiply”></td>
</tr>

<tr>
<td>Division:</td>
<td><input type=”checkbox” name=”check[]” value=”divide”></td>
<td>Modulo:</td>
<td><input type=”checkbox” name=”check[]” value=”mod”></td>
<td>Exponent:</td>
<td><input type=”checkbox” name=”check[]” value=”expo”></td>
</tr>
</table>
<input type=”submit” value=”Calculate” name=”calc”>
</form>

<?php

$n1 = $_GET[‘num1’];
$n2 = $_GET[‘num2’];

if(isset($_GET[‘calc’]))
{

echo “Num 1 is: “.$n1.”<br> Num 2 is: “.$n2;

if (!isset($_GET[‘check’])) {
echo “<br>You haven’t selected any operation”;
exit;
}
else
{
foreach($_GET[‘check’] as $c) {

switch($c)
{
case “add”:
echo “<br><br>Addition is “.($n1+$n2);
break;

case “subtract”:
echo”<br>Subtraction is “.($n1-$n2);
break;

case “multiply”:
echo”<br>Multiplication  is “.($n1 * $n2);
break;

case “divide”:
echo”<br>Division is “.($n1/$n2);
break;

case “mod”:
echo”<br>Modulus is “.($n1%$n2);
break;

case “expo”:
echo”<br>Exponent is “.($n1 ** $n2); /* This will work only on php 5.6 and + versions*/
break;

default: echo “Can’t calculate”;
break;
}
}
}
}

?>
</body>
</html>

okay, so i have used get method so that you can see which data are passing in URL and in form action you can leave it blank because we are not passing any variable in another php file. Our page has no another page.

This is .PHP file. You can right click on your browser and check view page source.There you can’t see our script. In short php file can handle html tags too!

About the Code:

isset() : it checks weather submit(calculate) button is clicked or not.

again after calculate button clicked there is !isset() function is there is checks if you have not selected any checkbox.

then there is foreach loop it basically cover all our checkbox and help us which one is clicked with the help of switch statement.


Pretty much easier Right? Try to build your own and let us know if you have any trouble !