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
https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/
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.

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
}

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;

?>

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.