Photo Sharing Application in PHP with More Features- Adding Captions and Deleting Images

Hello Friends and Developers ;)

Last time we created a Simple Photo Sharing Application with a Image Upload Form where the User selects a single image and uploads it. After that he can view it in an album.

This is the link for that app

Photo Sharing application using php

This is the 2nd part for the same tutorial. In this part we will add more features to the application like

  • Adding Caption to a Image
  • Deleting a Image

So lets see the code for that.

The Image Upload Form that we see in the previous tutorial will remain same i.e UploadPhotos.php

We will make changes to the PhotoAlbum.php to include a form below every image with two Buttons, one for Adding a Caption to the Image and another for deleting the image.

Here comes the updated version of

PhotoAlbum.php

<html>
<head>
<title>My Photos</title>

<link rel="stylesheet" type="text/css" href="Includes/Style.css"/>

<?php include("Includes/db.php"); ?>

</head>

<body>

<?php

  $result = mysql_query("SELECT * FROM mp_photos order by id DESC");

	  echo "<table border='1' cellpadding='10' class='tab' align='center'>\n";
	  echo"<tr class='head'>
	  <td colspan='4'>My Photos</td>
	  </tr>";

		$x=0;

		while($x < mysql_num_rows($result))
	   {
		  $picid=mysql_result($result, $x, 'id');
		  $name=mysql_result($result, $x, 'name');
		  $caption=mysql_result($result, $x, 'caption');

		  if($x%4==0)
		   {
			echo "<tr class='bod'>\n";
		   }

		   //To Be Able to Add Caption and Delete the Images we created a form below an image with two Buttons for adding caption and Deleting the image
			echo "<td align='center' bgcolor= '#E0FFFF' onmouseover=\"bgColor ='Turquoise'\" onmouseout=\"bgColor ='#E0FFFF'\">";
			echo "<img src = '$name' height='150' width='150' />
			<br/>$caption

			<form action='PhotoAlbumEdit.php' method ='post'>\n
					<input type='hidden' name='picid' value ='$picid'>\n

					<input type='Submit' value='Add Caption' name='add'/>
					<input type='Submit' value='Delete' name='delete'/>
			</form>
			</td>\n";

		  $x++;
		   if($x%4==0)
		   {
			echo "</tr>\n";
		   }
		}//end of while
		echo "</table><br /><br />";

	mysql_close($con);

?>

<p align='center'><a href='UploadPhotos.php'>Upload More Images</a></p>

</body>
</html>

PhotoAlbum_with_Edit_Buttons

PhotoAlbum_with_Edit_Buttons

PhotoAlbumEdit.php

This php page opens a form for adding a caption when the Add Caption Button is clicked.
It also removes the image if the Delete Button is clicked.

Adding a Caption to the Image

For adding a caption to the image we created a small Form below every image with a Button – Add Caption
When the user clicks that button the image opens in another page. There we created a form with a Text Box for adding the caption for the image. When the user add some caption and click on the save button the caption gets saved associated to that image in the Database.

Deleting an Image

When the user clicks on the Delete Button below an image the image gets deleted without giving any warning.

PhotoAlbumEdit.php

<html>
<head>
<title>My Photos</title>

<link rel="stylesheet" type="text/css" href="Includes/Style.css"/>

<?php include("Includes/db.php"); ?>

</head>

<body>

<?php

	$id = $_POST['picid'];

	/*Code for the Add Caption Submit button*/
		if (isset($_POST['add']))
		{
			/*SQL for getting the neccessary image details*/
		$res = mysql_query("SELECT * FROM mp_photos WHERE id=$id");

		/*Creating a form for adding Caption to the image*/

		echo "<form action='PhotoAlbumUpdate.php' method='post'>\n
		<table cellpadding='10' class='tab' align='center'>\n
		<tr class='head'>\n
		<td colspan='2' align='center'>Add Caption</td>
		</tr>\n";
		/*A while loop is used to display the image Caption in a text box*/
		while($row = mysql_fetch_array($res))
		  {
			echo "<tr>\n
			<td colspan='2' align='center'><img src = '{$row['name']}' /></td>\n
			</tr>\n";

			echo "<tr class='bod'>\n
			<td align='right'><p class='upd'>Caption: </p></td>\n
			<td><input type='text' name ='caption' size='50' maxlength ='150' value='{$row['caption']}'> </td>\n
			</tr>\n";

			echo"<tr>\n
			<td colspan='2' align='center'><br/>
			<input type='hidden' name='picid' value ='$row[id]'>
			<input type='Submit' value =' Save ' name='save' class='sub'> </td>
			</tr>";
		}
		  /*End of While loop*/
		echo "</table>";
		echo "</form>";

	}//Code for Add Caption Button Ends here

	//Code for Delete Button
	else if (isset($_POST['delete']))
	{
			$delete_query="Delete from mp_photos where id='$id'";

			if (!mysql_query($delete_query,$con))
			{
				die('Error: ' . mysql_error());
			}
			echo "Successfully Deleted";

			echo "<br /><a href='PhotoAlbum.php'>View Album</a>";

	}//Code for Delete Button Ends here

	mysql_close($con);
?>

<p align='center'><a href='UploadPhotos.php'>Upload More Images</a></p>

</body>
</html>

AddCaptionForm

Add Caption Form

AddingCaption

Adding Caption

Successfully Updated Msg

ViewAlbumWithCaption

View Album With Caption

PhotoAlbumUpdate.php
This php page takes the Caption for the image from the PhotoAlbumEdit.php and saves it to the database.

<html>
<head>
<title>My Photos</title>

<link rel="stylesheet" type="text/css" href="Includes/Style.css"/>

<?php include("Includes/db.php"); ?>

</head>

<body>

<?php
	$id = $_POST['picid'];

	/*Code for the Save Caption Submit button*/

	if (isset($_POST['save']))
	{
			$caption =$_POST['caption'];

			$update_query="Update mp_photos set caption='$caption' where id='$id'";

			if (!mysql_query($update_query,$con))
			{
				die('Error: ' . mysql_error());
			}
			echo "Successfully Updated";

			echo "<br /><a href='PhotoAlbum.php'>View Album</a>";

	}

		mysql_close($con);

?>

<p align='center'><a href='UploadPhotos.php'>Upload More Images</a></p>

</body>
</html>

Photo Sharing Application using PHP

I know all of you must have uploaded your images on various sites like Facebook, Picasa, Flickr etc. And you all must have wondered how it works.

So I thought of sharing a simple Photo Sharing Application in PHP which I made a long time ago.

Here in this PHP Application we have an Image Upload Form where the user can select a single image at a time and upload it. After that the user can see the image in an Album.

That’s it I have tried KISS (Keep It Short and Sweet).

In the later part we will try out more functionality like adding a caption to the image, creating different albums, deleting an image etc.

I have created a Directory called PhotoSharingSimple inside www directory of wamp.

C:\wamp\www\PhotoSharingSimple

Inside this PhotoSharingSimple I have created a directory called upload where I am saving all the images.

For this app we have created a Database in MySQL – myphotos

In that we have a created table called mp_photos

Structure of the table is given below

Table:mp_photos

Field

Type

Null

Extra

id bigint(20) No AUTO_INCREMENT
album bigint(20) No
name text No
caption longtext No

 

When the user select an image and click on the Upload Button, the image gets uploaded to the Server.

Using the global PHP $_FILES array, you can upload files (here images) from a client computer to the remote server.

Here in this App we are uploading the image to the Server and storing it’s path in the Database Table mp_photos.

When we want to display the images in an album, we are fetching their path from the Database and displaying them using the <img> HTML element.

db.php
We have created the DB Connection in a separate file called db.php. This helps in making any changes to the DB Code easier.

<?php
$con = mysql_connect("localhost","Anna","secretpwd");
   if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
  
  mysql_select_db("myphotos", $con);
?>

Style.css

We have kept the styles in a separate file and used them in all our PHP Files.


table.tab
	{
	border: 1px solid blue;
	text-align: center;
	border-collapse:collapse;
	background: #E0FFFF;
   }
   
tr.head{font-family: Tahoma,Arial; font-size: 15pt; font-style: normal; font-weight:; color: white; 
text-align: center; background-color:#00CED1}

tr.bod{font-family:Tahoma, Arial; font-size: 11pt; font-style: normal; font-weight: ; color:black}

input.sub{font-family:Tahoma, Arial; font-size: 14pt; font-style: normal; font-weight: ; color:black}
  

UploadPhotos.php

<html>
<head>
<title>Photo Sharing &lt; My Photos</title>

<link rel="stylesheet" type="text/css" href="Includes/Style.css"/>

<script type="text/javascript">
function displaymessage()
{
var x=document.forms["UploadForm"]["file"].value

 if(x==null || x=="")
 {
  alert("Select an Image first");
  return false;
  }
 }
</script>

<?php include("Includes/db.php"); ?>

</head>

<body>

<form name="UploadForm" method="POST" action="UploadPhotos.php" enctype="multipart/form-data" onsubmit="return displaymessage()">
		
	<table border='0' width='400' cellspacing='10' cellpadding='05' align='center' class='tab'>
		<tr class='head'>
			<td colspan='2' align='center'>Upload Single Image</td>
		</tr>

		<tr class='bod'>
			<td><label for="file"><br />Image:</label></td>
			<td><input type="file" name="file" id="file" /></td>
		</tr>
		
		<tr>
			<td colspan='2' align='center'><br /><br /><input type="submit" name="upload" value="Upload" class='sub' /></td>
		</tr>
	</table>
</form>


<?php
//Uploading Images when upload button is clicked
if (isset($_POST['upload'])){
	$dirname = "upload";


if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")))
{
	
if ($_FILES["file"]["error"] > 0)
 {
 echo "Error: " . $_FILES["file"]["error"] . "<br />";
 }
else
  {
  $filename = $_FILES["file"]["name"];
  echo "Uploaded Image : $filename <br />";
  echo "Type : " . $_FILES["file"]["type"] . "<br />";
  echo "Size : " . $_FILES["file"]["size"] . "  Kb<br />"; 
  //echo "Temp file: " . $_FILES["file"]["tmp_name"]. "<br/>";
  
   echo "<img src='$dirname/$filename' alt='$filename' height='200' width='200' />";
  
  //if you don't want to allow same images getting uploaded again then remove the comments.
  if (file_exists("$dirname/" . $_FILES["file"]["name"]))
   {
      //echo $_FILES["file"]["name"] . " already exists. ";
      //stopping same image getting added more than once.
	  //die('' . mysql_error());
   }
    else
   {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "$dirname/" . $_FILES["file"]["name"]);
      //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      //echo "<img src='upload/$filename' alt='$filename'>";
      
	}
	
	$sql_upload="INSERT INTO mp_photos(name)
		VALUES ('$dirname/$filename')";
  
	if(!mysql_query($sql_upload,$con))
	{
	die('Error: ' . mysql_error());
	}
	
	echo "<br /><br />Image Successfully Uploaded";	
	  
  }
  
  echo "<br /><br /><a href='PhotoAlbum.php'>See Album</a>";
  
  }//if the image is a valid type
  
  else
  {
	echo "Invalid file";
  }
  
  }//upload
  
mysql_close($con);
?>

</body>
</html>


Snapshot of Uploading an Image

Image Upload Form

Selecting the image for uploading

Uploaded Image


PhotoAlbum.php
<html>
<head>
<title>My Photos</title>

<link rel="stylesheet" type="text/css" href="Includes/Style.css"/>

<?php include("Includes/db.php"); ?>

</head>

<body>

<?php

  $result = mysql_query("SELECT * FROM mp_photos order by id DESC");
  
	  echo "<table border='1' cellpadding='10' class='tab' align='center'>\n";
	  echo"<tr class='head'>
	  <td colspan='4'>My Photos</td>
	  </tr>";
	  
		$x=0;
		
		while($x < mysql_num_rows($result))
	        {	
		  $picid=mysql_result($result, $x, 'id');
		  $name=mysql_result($result, $x, 'name');
		  $caption=mysql_result($result, $x, 'caption');
		    
		  if($x%4==0)
		   {
			echo "<tr class='bod'>\n";
		   }
		    
			echo "<td align='center' bgcolor= '#E0FFFF' onmouseover=\"bgColor ='Turquoise'\"            onmouseout=\"bgColor ='#E0FFFF'\">";	
			echo "<img src = '$name' height='150' width='150' />	
			<br/>$caption			
			</td>\n";	
			
		  $x++;
		   if($x%4==0)
		   {
			echo "</tr>\n";
		   } 
		}//end of while
		echo "</table><br /><br />";

    mysql_close($con);
		
?> 

<p align='center'><a href='UploadPhotos.php'>Upload More Images</a></p>

</body>
</html>

Snapshots of the Album

View Album

Hope you enjoyed this application. Keep Coding :D

What are SOPA and PIPA?

SOPA (Stop Online Piracy Act) and PIPA (Protect Intellectual Property Act ) are bills in the U.S. House of Representatives and the U.S. Senate, respectively.

These bills are presented as efforts to stop copyright infringement committed by foreign web sites, but  they do so in a way that would disrupt free expression and harm the Internet.

To know more visit the links below

WikipediaGoogleMashable

Girls Beware- Famous Questions just asked to girls during interviews

Whether you are a IT Professional or some other career woman these are some of the famous questions mostly asked to a woman during a interview.

So What are your marriage plans?

When are you getting married?

How old are you?

Are you comfortable with relocation?

Will your parents allowed you to go to another city, another country for onsite work?

Come on ladies if you really wanted the job, you need to answer effectively. Everybody have to get married one day but show some enthusiasm for the company, that you really want to work for it that you will be a good employee and want to give your 100%.

Hardly anybody will answer that they will get married as soon as they get the job.

It’s 2012, women work shoulder to shoulder with men. They are doing even better in their various professional roles.

A few problems that Employers fear while hiring woman employees

  • Leave the company once married
  • Maternity Leaves
  • Can’t work in late night shifts

Yes these are common things with woman but in this age, many woman works much better than men and many like to work more than taking up family responsibilities. And there are many woman out there who are handling both their work and household efficiently. So don’t let anything go against you. Show your spirit that you value your company and the job will be all yours.

Harbinger Group IT Company, Pune Selection Process 2011

Harbinger Selection Process for Software Developer, Software Testers mostly starts at July every year.

The Selection Process can be divided into the following categories:

  • Selection of students based on their profiles
  • Presentation about Harbinger at their office
  • Written Test
  • Technical/HR Interviews

Selection of students based on their profiles

Harbinger directly communicates with the Placement Cell of the college whose students it wants to hire. They ask for student details like Name, Address, Email, Class Xth, XIIth, Graduation and PG Marks.

Based on these 10 students are shortlisted of any college and called for an aptitude round at Harbinger Office. The number can vary. Harbinger doesn’t have any criteria like 60% throughout. But you have to do well in all the rounds.

Harbinger selects 10 students each from various colleges in Pune doing MCA, MSc IT, MSc CA, MCS. These students have to appear for a written test.

So you have to compete not only with your own class mates but also with the students of other colleges.

Presentation about Harbinger at their office

In their office, first they give a small Presentation about Harbinger followed by a Q/A session. It can continue for about 1 hour. Be attentive during this because the HR looks for potential candidates during this time and many questions can come in interviews later on based on this presentation.

I suggest make a good study about the company before going for its selection process. It does make a lot of difference. The employers feel that this candidate is really interested in joining our company and chances of your getting selected increases. Find out what work the company does, where is it based, what are its products, who are the Chairman etc etc. It does helps a lot during interviews.

Written Test

After that you have to give a written test of 1 hour. Nature of this test is as below

Nature of Test:                 (3 Sections in total: Duration 1 Hour)

  • Section 1: Quantitative Aptitude – 10 Multiple Choice Questions
  • Section 2: Computer Fundamentals- – 10 Multiple Choice Questions
  • Section 3: Computer Programming: Either in C, C++ or Java – You need to write the code of the    Program

There is a negative marking in this test. For every correct answer you will get +1 mark and for every wrong answer -1. This is applicable for Section 1 and 2 only, No negative marking for section 3. There is a sectional cut off in the evaluation of answer sheets

Here’s a brief description about how these sections are going to be

  • Quantitative Aptitude- Practice Time And Work, Time and Distance, Profit and Loss, Probability etc etc. If you are good in Maths you are through this one.
  • Computer Fundamentals- Revise your Data Structures Stacks, Queues, Trees, Linked Lists, Sorting and Searching etc.
  • SQL Fundamentals should be clear. Get Ready to write some sql queries or you may need to write the o/p of queries already given. Focus on Joins, they are a favourites in every selection process.
  • Harbinger also tests your OOAD concepts like Association.
  • You may have to draw different UML Diagrams Class, Object) based on a given case study.
  • I suggest you brush up all these concepts well before coming for the selection process.
  • Computer Programming- Get ready to write some Java or C++ Program in this section. The question is going to be tough and will test your capability to design algorithms for a given problem

Overall the test is not that tough and if you really love Computers and Programming you should get through it. But remember as there will be many students from other colleges also, the competition is going to be large and you need to give something extra to make the cut.

Only 3 or 4 students from a college are called for interviews based on their performance in this round.

After the written test you can leave the Harbinger Office either feeling confident that you will get the call for the interview or thinking where I stand in the crowd and how should I improve myself.

No problem Winning and Losing are part and parcel of the life. The sooner you understand this, the better for you.

It would be better if you spent this day preparing for the interview.

Technical/HR Interviews

Next Day the result of the written test is mailed to respective college TPO. Candidates shortlisted after the Written Test are asked to come for an interview at the Harbinger office with a printed copy of their CV, and one passport size photograph.

Technical interviews are tiring. You may have to wait for your turn for hours and then I can continue for hours more. Be prepared to explain the code that you have written yesterday, how you approached the solution, why did you use this language etc.

They may ask you to write some programmes there itself. Most Technical interviewer’s sits with Pen and Paper with them. SQL Queries are also popular questions. Revise Joins properly.

Sometimes interviews can happen as late as 10’0 clock in the night. Don’t panic. Just keep your cool and speak confidently. You know it most of the things. It’s your time now to express them before a group of people who have got more experience and knowledge than you.

After interview, you can say whether you are selected or not. Sometimes they announce the result that day only, sometimes the results are sent by mail to your college.

Those who get selected rejoice. Those not try for the next company visiting their campus.

All the Best for your Harbinger selection process.  It’s a nice company to do a starting in your flourishing IT Career. :D