Jump to content

[SOLVED] Restricting File Types


stevesimo

Recommended Posts

Hi, I am attempting to use some code I found in an article on this site to upload an image to the server.  I have got this working although I need to be able to restrict the image formats to GIF, JPG and PNG.

 

I have got it working where it will upload a GIF but not a PNG or JPG.  Anyone know how I fix this?

 

Here is my code:

 


if(isset( $Submit )) { 
  if ($_FILES['imagefile']['type'] == "image/gif" OR $_FILES['imagefile']['type'] == "image/png" OR $_FILES['imagefile']['type'] == "image/jpg"){ 
    copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name'])  
    or die ("Could not copy"); 
    echo "";  
    echo "Name: ".$_FILES['imagefile']['name']."";  
    echo "Size: ".$_FILES['imagefile']['size']."";  
    echo "Type: ".$_FILES['imagefile']['type']."";  
    echo "Copy Done....";  
  }else{ 
    echo "<br><br>"; 
    echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>"; 
  } 
} 

 

Thanks

 

Steve

Link to comment
https://forums.phpfreaks.com/topic/50499-solved-restricting-file-types/
Share on other sites

I managed to fix it using an array containing all the different MIME types

 

$allowed = array ('image/gif', 'image.jpeg', 'image/jpg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/bmp');

 

  if (in_array($_FILES['imagefile']['type'], $allowed)){

    //do something with image

  }

 

 

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.