Jump to content

limit file type help


lucan

Recommended Posts

 

I have this code which uploads fine but I am trying to limit the type of files users can upload.  For some reason this code still let any file type to be uploaded being very new to php any help would be Appreciated.

 

<?php

if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/pjpeg"))
&& ($_FILES["image"]["size"] < 20000))
  {
  if ($_FILES["image"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["image"]["name"] . "<br />";
    echo "Type: " . $_FILES["image"]["type"] . "<br />";
    echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["image"]["tmp_name"];
    }
  }

$target = "testimages/"; 
$target = $target . basename( $_FILES['image']['name']); //


$company=$_POST['company']; 
$image=($_FILES['image']['name']);


mysql_connect("localhost", "user", "pass") or die(mysql_error()) ; 
mysql_select_db("testupload") or die(mysql_error()) ; 

mysql_query("INSERT INTO `table` (company, image) VALUES ('$company', '$image')") ; 

if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) 
{ 


echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
} 
else { 

echo "Sorry, there was a problem uploading your file."; 
} 

?> 

Link to comment
https://forums.phpfreaks.com/topic/229921-limit-file-type-help/
Share on other sites

You need an else statement on your very first conditional.  If you add an else statement to this that exits the script you'll be fine.  The one that says you're not an image or below 20000 bytes.  Or just move all the rest of the code into the first if.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.