Jump to content

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

  }

 

 

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.