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
Share on other sites

With this code at the moment it does upload the file correctly.  However, i have try a number of ways to restrict the different files with this code and thats when i have the problem you stated "uploads all file but only displays jpg file in the correct formate. 

 

 

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.