user3224929
user3224929

Reputation:

Adding File Type restriction in my upload script

I have an upload script that is works like a charm for uploading the files. I have also setup the upload size limit in the script. But I want to set up the file type restriction in my script but I am confused in implementing it. Can anyone help me with this? I want to allow only JPG, JPEG, PNG, GIF, PDF and DOC files. How can I do this? Please help.

Here is my upload script.

<?php 
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("project101test",$conn) or die(mysql_error());



if(isset($_POST['submit'])!=""){
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];


$limit_size=512000; // Define file size limit in Bytes.
$size_in_kb=1024; // File size in KB
$divide=$limit_size/$size_in_kb; // Dividing both the variables to get the size in KB.


if($size > $limit_size){
echo "Your file size is over limit<BR>";
echo "<BR>File size limit = $divide KB";
}

else {
move_uploaded_file($temp,"admin/files/".$name);

$insert=mysql_query("insert into upload(name, fname, phone, email, message)values('$name','$_POST[fname]','$_POST[phone]','$_POST[email]','$_POST[message]')");
}



if($insert){
echo "File Uploaded";
}
else{
die(mysql_error());
}
}
?>

PHP EXPERTS please help me.

Upvotes: 0

Views: 80

Answers (1)

mohamed_i
mohamed_i

Reputation: 26

Try to check the condition with the file extension

$extension = substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'], '.'));

 $extension = strtolower($extension);

if( $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif" ||$extension == ".png" )
{
 // File upload code
}

Upvotes: 1

Related Questions