How to upload only Image file (jpg, jpeg, png, gif) through PHP ?

<!DOCTYPE html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title></title>
 <style>
 input[type='submit']{background:rgba(0,102,255,1); border:1px solid rgba(0,102,255,1); border-radius:5px; width:100px; height:30px; font-family:roboto; color:rgba(255,255,255,1); font-size:18px; cursor:pointer;}
 </style>
 </head>
<body>
 <center>
 <form action="select.php" method="post" enctype="multipart/form-data">
 <input type="file" name="image"/>
 <input type="submit" name="submit" value="Upload"/>
 </form>
 </center>
 </body>
 </html>
 <?php
 if(isset($_REQUEST['submit']))
 {
 $name=$_FILES['image']['name'];
 $ftype=$_FILES['image']['type'];
 $type=array("image/jpg","image/png","image/jpeg","image/gif");
if(!$name)
 {
 echo "Please select file";
 }
 else
 {
 $f=0;
 for($a=0;$a<4;$a++)
 {
 if($type[$a]==$ftype)
 {
 $f=1;
 move_uploaded_file($_FILES['image']['tmp_name'],"newfolder/".$name);
 echo "<a href=newfolder/$name target='_blank' buffer='write'> View file </a>";
 echo "<img src=newfolder/$name width='300px' height='350px' />";
 break;
 }
 }
 if($f==0)
 {
 echo "Invalid File type.<br/>";
 echo "Your file Type is ".$name;
 }
 }
 }
?>

<!– MAKE SURE THAT YOU CREATE A FOLDER IN THE SAME AREA WHERE YOU SAVE YOUR ‘select.php’ FILE  AND NAME IT AS ‘newfolder’ WHERE YOUR ALL UPLOADED FILES WILL STORE –>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s