include() Vs require() in PHP

These two functions in php means the same functionality.But require() will result a fatal error but include() will give a warning.That means the error handling is different.Therefore when an error occurs in include(), it will display the error and the programme will not terminate (Because the error is a warning).But in require() function the program will terminate because it will give a fatal error…

Published in: on October 15, 2009 at 10:31 am  Leave a Comment  

include() Vs include_once() in PHP

include_once() is safer than include()…

As an example, lets assume:

we use include() to use a file and that file has a function.Again I include another file and that file also has the same function.Then there will be a conflict of functions because the same function has been already added.But if we use include_once(), it will not be a problem,because it will avoid of verwitting.Therefore include_once() is a safe method to avoid functions from redefining and overwriting.Since we can use include() many more times , it will cause conflicts due to duplicating functions and variable value reassigning.

Lets discuss a very simple example to understand the difference between include() and include_once()

hello.php file will print “Hello”. we will use this in display.php file as follows…

hello.php

<?php
echo “Hello”;
?>

display.php

<?php
include(‘hello.php’); //this will print ‘Hello’
include(‘hello.php’); //This will again print ‘Hello’
include(‘hello.php’); //This will print ‘Hello’ for the third time also

include_once(‘hello.php’); //This will not diaply ‘Hello’ again
include_once(‘hello.php’); //This will not diaply ‘Hello’ again

?>

Published in: on October 15, 2009 at 9:18 am  Leave a Comment  

Again Started Blogging

Since I was away from blogging for last few months, again I feel bloging is a way of running behind the knowledge.When I addict to do blogging,everyday I am looking for something new.Therefore everyday I am trying to do something new and trying to post it..Yeah..my lovely blog… 🙂

Published in: on October 14, 2009 at 12:18 pm  Leave a Comment  

Navicat MySQL GUI

This is what I used to add multiple records into a table in MySQL database. Here I used an excel sheet to store data
as rows which represent the records of the table.

Following figures describes the way I have loaded data into the table using an excel sheet.This was an easy method for me to insert
multiple records into a table at once. 🙂

Open Navicat. Set the mysql connection.Then go to the databse and select the table where we need to insert data.

Select the file format we are going to import data.

I used an excel sheet ,therfore I had to select the sheet.

Data was inserted into the table from the excel sheet…. 🙂

Published in: on October 14, 2009 at 5:48 am  Leave a Comment  

Firefox DOM (Document Object Model) Inspector


DOM inspector for firefox is a tool which can be used to identify elements in web page.We can use this tool to inspect and edit the live DOM of any web document.The objects in a web document will be listed as hierachy, therefore for we can navitage among nodes very easily.

When we select the element , it will show the object in the web page (Highlight in red colour box around the particular object).Therefore this can be used to identify objects in a web page easily.

Published in: on October 14, 2009 at 4:08 am  Leave a Comment  

PHP mail()

In the php manual , it is simply mentioned as
<?php
$to = ‘bob@example.com’;
$subject = ‘Wakeup bob!’;
$message = ‘yo, whassup?’;
$headers = “From: server@example.com\r\n” .
‘X-Mailer: PHP/’ . phpversion() . “\r\n” .
“MIME-Version: 1.0\r\n” .
“Content-Type: text/html; charset=utf-8\r\n” .
“Content-Transfer-Encoding: 8bit\r\n\r\n”;
?>

// Send
mail($to, $subject, $message, $headers);

But sometiems this will not work due to the server’s php intial file’s settings.So what we can do is to overcome this problem,
we sould set the php ini settings as follows.Then it will work properly.

Following is an example which describes the php mail function with php ini settings.

This is the Form

This is the HTML code for the above form

<html>
<title>Comments</title>
<body>
<form method="POST" action="feedback.php">
<table>
<tr>
<td width="87">Name:</td>
<td width="242"><input name="name" size="25" type="text"></td>
</tr>
<tr>
<td>Email :</td>
<td><input name="email" size="25" type="text"></td>
</tr>
<tr>
<td valign="top"> <div align="left">Comments </div></td>
<td> <textarea rows="15" cols="25" name="comments"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"> <input name="submit" value="Send" type="submit"><input name="Clear" value="Clear" type="reset"> </td>
</tr>
</table>
</form>
</body>
</html>


This is the PHP code

<?php

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];

$headers .= "Content-type: text/html\r\n";
$subject = "Feedback From a Viewer";
//$mail = "someone@somewhere.com";
$additional_parameters = "-r $mail";

$message = "Name: $name<br><br> Email Address: $email<br><br> Comments: $comments ";

//
ini_set("SMTP","mail.servername.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","$email");
ini_set("display_errors",0);

if(mail("mails@hndesl.com","$subject","$message","$headers",$additional_parameters))
{
echo "Thank You For Your Feedback";
}
else
{
echo "Thank You For Your Feedback";
}

?>

Published in: on October 13, 2009 at 7:31 am  Leave a Comment  

Multiple Picture Upload

This is the Interface at the beginning

This is the interface after uploading

This code can be used to upload five pictures at the same time in to the server folder named “images.”When the pictures are uploaded , they will be resized and saved in to the server’s folder.And the uploaded pictures will be diaplyed in the browser.

(Note: Create a folder called “images” in the same folder where this file will be inserted.That is the folder where pictures are going to be uploaded.)

PHP Code

<?php
error_reporting(0);

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) – $i;
$ext = substr($str,$i 1,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
global $files;
$files = array('file','file2','file3','file4','file5');

for($i=0; $i<5; $i )
{
$file = $files[$i];

$image =$_FILES["$file"]["name"];
$uploadedfile = $_FILES["$file"]["tmp_name"];

if ($image)
{
$filename = stripslashes($_FILES["$file"]["name"]);
$extension = getExtension($filename);
$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}
else
{
$size=filesize($_FILES["$file"]["tmp_name"]);
if ($size > MAX_SIZE*1024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
else
{

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES["$file"]["tmp_name"];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES["$file"]["tmp_name"];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;
list($width,$height)=getimagesize($uploadedfile);

$newwidth=100;
$newheight= 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$filename = "images/small_". $_FILES["$file"]["name"];
$filename1 = "images/". $_FILES["$file"]["name"];
echo "<img src=' $filename ' />&nbsp;&nbsp;&nbsp;";
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}
}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
$change=' Images Uploaded Successfully!';
}

?>

HTML code

<html>
<head><title>Multiple Picture Upload</title>
</head><body>
<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file2" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file3" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file4" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file5" type="file" /></td>
</tr>
<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table></body>
</html>

Published in: on October 13, 2009 at 5:47 am  Leave a Comment  

Unable to Start WAMP or XAMPP server

One day I was trying to start WAMP sever. I was 100% sure about my configuration. There was no problem with the configuration settings. But when I was trying to start WAMP, I couldn’t do it. WAMP server was not started.

At this time I got into know that….
IF it is WAMP or XAMPP, those servers cannot be started if Skype is running. This is what I found on this.

Therefore the conclusion is if Skype is running then WAMP/XAMPP cannot be started. But after starting WAMP/XAMPP server skype can be started. Then both will run without any problem… 🙂

Published in: on October 12, 2009 at 4:19 pm  Leave a Comment  

Image Upload

Interface After Uploading Images

PHP Code

<?php
error_reporting(0);

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) – $i;
$ext = substr($str,$i 1,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];

if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
//set the file types which are acceptable
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")&& ($extension != "bmp")&& ($extension != "TIFF"))
{
$change='Unknown Image Extension';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
$change='You have exceeded the size limit!';
$errors=1;
}
else
{

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;
list($width,$height)=getimagesize($uploadedfile);

//set the file size according to your choice
$newwidth=100;
$newheight= 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

//the original image
$originalWidth=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($originalWidth,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$originalWidth,$originalHeight1,$width,$height);

//set the name of the file
$filename = "small_". $_FILES['file']['name'];
$filename1 = $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}

//If no errors registred, print the success
if(isset($_POST['Submit']) && !$errors)
{
$change='Image Uploaded Successfully!';
}

?>

HTML Code

<html>
<head>
<title>Picture Upload</title>
</head>
<body>

<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td><img src="<?php echo $filename; ?>" /> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>

<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table>

</body>
</html>

Published in: on October 12, 2009 at 12:33 pm  Leave a Comment  

HTTP Upload

HTTP is a protocol which is a set of instructions. This tells the browser how to send data to the server. HTTP supports two methods named GET and POST. Among these two methods, POST is the method mostly used because it has no limit to how much data can be sent via this method.

In HTTP Upload, when we upload the file, it is taken by the browser. Then this specified file will be sent to the server via HTTP POST. Then the server takes that data and hand it off to the framework to handle. This will turn the POST data into a file and variables to pass to the server side script. That means the file will be stored in a temporary storage area in the server. Then we can use a server side script (eg.PHP) to handle this file.

Now the file has been already uploaded. Then all it can do is decide whether to save this file some where or let it be discarded in the normal clean up. If the file is not moved to a different location from the temporary location, it will be cleaned up automatically.

Published in: on March 20, 2009 at 4:32 am  Leave a Comment