Validating Image files using php

Links for php scripts

Moderators: macek, egami, gesf

amruth
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Jan 06, 2012 9:23 am

Validating Image files using php

Postby amruth » Fri Jan 06, 2012 9:56 am

Hi,
I am posting the file from client side HTML <input type=file...> and I wrote a PHP script at server side to check whether the uploaded file is image or not as if($_FILES["file"]["type"]=="image/jpg"....).
But this code does not check the actual content of the file uploaded,i.e if someone renames other file types as .jpg or .png etc then my script takes it as image file.

Anybody please help me.

Thanks.

soumitra
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jan 05, 2012 9:23 am

Re: Validating Image files using php

Postby soumitra » Mon Jan 09, 2012 4:42 am

File Upload in PHP

----------------Save it any name--------------------
<html>
<head>
<title>img upload</title>
</head>
<body>
<form name="frm" method="post" action="upload2.php" enctype="multipart/form-data">
<table>
<tr>
<td>Select File </td>
<td><input type="file" name="img" value="" /></td>
</tr>
<tr>
<td><input type="submit" name="sub" value="UPLOAD" /></td>
</tr>
</table>
</form>
</body>
</html>

ANOTHER PAGE NAME---upload2.php.

<?php
$img=$_FILES['img']['name'];
$size=$_FILES["img"]["size"];
$dir="image/";
$total_dir=$dir.$img;
//cheack
$imginfo=pathinfo($img);
//$basename=$imginfo['basename'];
$extension=$imginfo['extension'];
if ($extension == "jpg" || $extension == "flv" )
{
//size cheack max size=2m.b (2097152 byte)
if ($size<="2097152")
{
//Uploading
$p=(move_uploaded_file($_FILES['img']['tmp_name'],$total_dir));
if ($p)
{
echo "UPLOAD secessful";
}
else echo "NOT UPLOAD";
}
else
echo "invalid size";
}
else
echo"INVALID file format";
?>


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest

cron