PHP Code

To upload a file using php

Steps:

1. Create new html file and copy upload.html code and save it as upload.html.

2. Create new php file and copy uploader.php code and save it as uploader.php.

3. Run upload.html, then it will display the html data.

4. Click browse button and select any file or image and press upload button.

5. Then it will transfer that specific file or image to the created folder and display the output as file is uploaded.

upload.html:

[php]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>upload</title>
</head>

<body>
<form enctype="multipart/form-data" method="post" action="uploader.php">
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
</body>
</html>

[/php]

It displays the output as:


uploader.php
[php]</form><form action="uploader.php" method="post" enctype="multipart/form-data"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head><body>
<?php
if(isset($_REQUEST[‘submit’]))
{
$to="uploaded/".$_FILES[‘file’][‘name’];
move_uploaded_file($_FILES[‘file’][‘tmp_name’],$to);
echo "file is uploaded";

}
?>
</body>
</html>

[/php]

//create a new folder uploaded

Tags:

Calculate add,sub,mul using php

Steps:

1. Create new html file and copy calculate.html code and save it as calculate.html.

2. Create new php file and copy calculation.php code and save it as calculation.php.

3. Run calculate.html, then it will display the html data.

4. Enter value1, value2 and press add or sub or mul button and press calculate button.

5. Then it will transfer to calculation.php which displays the output.

calculate.htm:

[php]

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>calculation</title>
</head>

<body>
<form method=”post” action=”calculation.php”>
Value 1: <input type=”text” name=”val1″ size=”7″><br /><br />
Value 2: <input type=”text” name=”val2″ size=”7″>
<p>
<input type=”radio” name=”calculate” value=”add”  />
add
<input type=”radio” name=”calculate” value=”sub”  />
sub
<input type=”radio” name=”calculate”  value=”mul”/>
mul
</p>
<input type=”submit” name=”submit” value=”calculate”/>
</form>
</body>
</html>

[/php]

It displays the output as:
Value 1: Value 2:
add
sub
mul

calculation.php:
[php]
</form><form action=”calculation.php” method=”post”><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head><body>
<?php
if(($_POST[‘val1’]==””) || ($_POST[‘val2’]==””))
{
header(“location: calculate.html”);
exit();
}
if($_POST[‘calculate’]==”add”)
{
$result=$_POST[‘val1’]+$_POST[‘val2’];
}
else if($_POST[‘calculate’]==”sub”)
{
$result=$_POST[‘val1’]-$_POST[‘val2’];
}
else if($_POST[‘calculate’]==”mul”)
{
$result=$_POST[‘val1’]*$_POST[‘val2’];
}
echo “Calculation is: $result”;
?>
</body>
</html></form><form action=”calculation.php” method=”post”>
[/php]
An example output for add by giving the input as value1 as 5 and value2 as 5 is:
Calculation is: 10

Tags:

Transfer data from one page to another using php

Steps:

1:Create new php file and copy file1 code and save it as file1.php.

2.Create another new php file and copy file2 code and save it as file2.php.

3.Run file1.php, then it will display the file1 data.

4.Enter your name, age and location and press the submit button.

5.It will transfer to file2.php and displays the output.

file1:

[php]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>table1</title>
<script language="javascript" type="text/javascript">
function check()
{
if(document.getElementById(‘name1’).value==”)
{
alert("Please enter your name");
document.getElementById(‘name1’).focus();
}
else if(document.getElementById(‘age1’).value==”)
{
alert("please enter your age");
document.getElementById(‘age1’).focus();
}
else if(document.getElementById(‘location1’).value==”)
{
alert("please enter the location");
document.getElementById(‘location1’).focus();
}
else
{
document.form.submit();
}
}
</script>
</head>

<body>
<form method="post" action="file2.php" name="form" >
<table width="100%" border="0">
<tr>
<td><label>Enter your name: </label>
</td>
<td><input type="text" name="name"  id="name1"; />
</td>
</tr>
<tr>
<td><label>Enter your age:</label>
</td>
<td><input type="text" name="age" id="age1"; />
</td>
</tr>
<tr>
<td><label>Enter the location:</label>
</td>
<td><input type="text" name="location" id="location1"; />
</td>
</tr>
<tr>
<td colspan="2"> <div align="center">
<input type="button" value="Submit" onclick="check();" name="Submit" />

</div></td>
</tr>
</table>
</form>
</body>
</html>

[/php]

//save this as file1.php

It will display the output as:

file2:

[php]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>table2</title>
</head>

<body>
<?php
$var1=$_POST[‘name’];
$var2=$_POST[‘age’];
$var3=$_POST[‘location’];
echo "your name is: $var1"."<br/>";
echo "your age is: $var2"."<br/>";
echo "loaction is: $var3";
?>
</body>
</html>

[/php]

//save this as file2.php

after entering your name, age and location and pressing the submit button, it displays the output.

For example:

your name is: John
your age is: 25
loaction is: CA

Tags: , ,

To display the current day and image for a particular day

[php]
<?php
$today=date(l);
if($today==Monday)
{
echo "Today is Monday" ."<br/>";
echo "<img src=’images/image1.gif’>";
}
elseif($today==Tuesday)
{
echo "Today is Tussday" ."<br/>";
echo "<img src=’images/image2.gif’>";
}
elseif($today==Wedsday)
{
echo "Today is Wedsday" ."<br/>";
echo "<img src=’images/image3.gif’>";
}
elseif($today==Thursday)
{
echo "Today is Thursday" ."<br/>";
echo "<img src=’images/image4.gif’>";
}

elseif($today==Friday){
echo "Today is Friday"."<br/>";
echo "<img src=’images/image5.gif’>";
}

elseif($today==Saturday){
echo "Today is Saturday"."<br/>";
echo "<img src=’images/image6.gif’>";
}

elseif($today==Sunday){
echo "Today is Sunday"."<br/>";
echo "<img src=’images/image7.gif’>";
}
?>
[/php]

//create the folder name as images and store the 7 images. So, it will display the todays image.

It display the output as:

Today is Friday

Tags:

To display current date,no.of days and weeks in a month and year using php

1:Create new php file and copy the code and save it as file1.php.

2.Run file1.php, then it will display the current date and no.of days,weeks in a month and year.

[php]
<?php
echo  "My name is: John"."<br/>";
echo "current date is:"."\n" .date("Y m d h: s: m") ."<br/>";
echo "No of days in a month:"."\n" .cal_days_in_month( CAL_GREGORIAN, 07, 2010) ."<br/>";
echo "No of weeks in a month:"."\n".date("w", mktime(0,0,0,12,31,2010)) ."<br/>";
echo "No of weeks in a year:"."\n" .date("W", mktime(0,0,0,12,31,2010)) ."<br/>";
?>
[/php]

It display the output as:

My name is: John
current date is: 2010 07 23 04: 46: 07
No of days in a month: 31
No of weeks in a month: 5
No of weeks in a year: 52

Tags:

Php code to find leap year or not

We can find a year is leap year or not in various ways.we can use the following fuctions in php.

  • By using date() function
  • By Modulus operator

Here is a sample php code that uses modulus(%)operator for finding the leap year.

Write the below code within php tags

[php]
<code>
<!–?PHP </p–> </code>

<code>$start = 1000;
$end = 1011;
for($i = $start; $i < $end; $i++)
{
if(($i%4) == 0){$val = "Leap Year";}else{$val = "Not Leap Year";}
echo $i, ‘ —> ‘.$val.”;
}</code>

?>
[/php]

It will produce the output as given below.

1000 —> Leap Year
1001 —> Not Leap Year
1002 —> Not Leap Year
1003 —> Not Leap Year
1004 —> Leap Year
1005 —> Not Leap Year
1006 —> Not Leap Year
1007 —> Not Leap Year
1008 —> Leap Year
1009 —> Not Leap Year
1010 —> Not Leap Year

You find leap years for any year ranges by changing the $start and $end.

Tags: ,

CGI Time Out The specified CGI application exceeded the allowed time for processing. The server has deleted the process.

Whenever we tried to run some large script like data extraction or trying to get thousand number of records from remote database, most of us will face the following CGI Issue

“The specified CGI application exceeded the allowed time for processing. The server has deleted the…”

In order to run your script without any such kind of issue, please follow the steps provided below. Earlier I too faced such a issue but after research in my PC I finally fixed it.

Note: the following steps is only suitable only for the Windows XP and with the IIS version 5.1:

Go to your Internet Information Services (IIS) page (Go Start -> Settings -> Control Panel -> Administrative tools -> Internet Information services)

Unfold Websites->Default Web site

Now right click on your website name (on which you are executing your script)

Select Properties

Select the tab Virtual Directory

In that page, select the option High (Isolated) under Application Protection, this option will remove the default timeout settings.

Now click on the button Configuration

Then select Process Options tab

Change the CGI Script Timeout value max value is 999999.

That’s it. You may now run your script without any issue.

Tags: ,

Request a Free SEO Quote