Jump to content

Help with upload file types allowed


hash1

Recommended Posts

Hey guys im working on my first script that will actually be useful to people, however im wondering how I can only allow users to upload the following file formats: .zip, .rar, and .7zip

 

Here is the code that takes care of that, but I feel I have done it incorrectly.

if ((($_FILES["file"]["type"] == ".zip")
|| ($_FILES["file"]["type"] == ".rar")
|| ($_FILES["file"]["type"] == ".7zip"))
&& ($_FILES["file"]["size"] < 20000))
  {

 

I also want to change the maximum file size to something in the MB range instead of 20 kilobytes also but am unsure how to.

 

Thanks!

Link to comment
Share on other sites

To change the maximum upload size you have to edit your php.ini.  I think the parameter you want to edit is post_max_size or something like that.  As for checking the file type, you should just do:

 

print_r($_FILES);

 

And see what it's spitting out, and then you can make sure you're checking the right thing.

Link to comment
Share on other sites

I think he means he wants to check the file type in the backend, not show the whole structure of the array.

 

as jcombs said, the type attribute of files is the mime type not the extension

mime type wiki article

 

if you want the extension, you can do something like

$extension = end(explode('.', $filename));

 

but the way you are checking can be slightly improved (not really for efficiency, but it will look cleaner) you can use in_array(), and make an array of allowed types, IE

$allowed = array("jpeg", "html", "etc");
//check if the file type is right
if (in_array($_FILES['file']['type'], $allowed)){
//continue
}
else {
//exit
}

Link to comment
Share on other sites

I think he means he wants to check the file type in the backend, not show the whole structure of the array.

 

as jcombs said, the type attribute of files is the mime type not the extension

mime type wiki article

 

if you want the extension, you can do something like

$extension = end(explode('.', $filename));

 

but the way you are checking can be slightly improved (not really for efficiency, but it will look cleaner) you can use in_array(), and make an array of allowed types, IE

$allowed = array("jpeg", "html", "etc");
//check if the file type is right
if (in_array($_FILES['file']['type'], $allowed)){
//continue
}
else {
//exit
}

 

keep in mind OP (as i'm sure it was just an oversight by mikesta), checking $_FILES['file']['type'] against the $allowed array will return false.  you can use mikesta's $extension to check against the array:

 

<?php
if (in_array ($extension, $allowed))
?>

 

or you will have to change the values in the $allowed array:

 

<?php
$allowed = array ('image/jpeg', 'text/html');

if (in_array ($_FILES['file']['type'], $allowed)) //will now return true if a jpeg or .html file is uploaded;

?>

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.