Jump to content

Limit uploads to certain file extensions


grahamb314

Recommended Posts

Hi all

I have a bit of code that uploads a file (works fine) to a directory (if the dir isn't there, then it makes it :-)

 

I need to only allow certain file types to be uploaded. (mp3, mp2, mp1, wav and ogg)

does someone know how to implement that into my code?

 

Thank you!!

 

<?php
$filename = "uploads/{$_SESSION['directory']}";

if (is_dir($filename)) {
    
echo "The folder: $filename exists";
echo "<br>";

foreach($_FILES as $file_name => $file_array) {

	if (is_uploaded_file($file_array["tmp_name"])) {

		move_uploaded_file($file_array["tmp_name"], $filename.'/'.$file_array["name"]) or die ("Couldn't copy");
		echo "The File: ".$file_array["name"]."<br/>\n";
		echo "Was uploaded successfully to:  ";
		echo $filename;
		/////////////////make a link to check? ////////////////////////
	}//file types allowed: mp3, mp2, mp1, wav and ogg

}

} else {
    

mkdir("{$filename}", 0700);
echo "The folder did not exist but has now been created";
echo "<br>";

foreach($_FILES as $file_name => $file_array) {

	if (is_uploaded_file($file_array["tmp_name"])) {

		move_uploaded_file($file_array["tmp_name"], $filename.'/'.$file_array["name"]) or die ("Couldn't copy");
		echo "The File: ".$file_array["name"]."<br/>\n";
		echo "Was uploaded successfully to:  ";
		echo $filename;
	}

}

}

?>

i have a code like that in my upload code for pictures, it goes as follows:

 

// Does the file have the right MIME type?

 

  if ($_FILES[$filename]['type'] != 'image/pjpeg' AND $_FILES[$filename]['type'] != 'image/jpeg' AND $_FILES[$filename]['type'] != 'image/gif' AND $_FILES[$filename]['type'] != 'image/png' AND $_FILES[$filename]['type'] != 'image/wbmp')

      {

      echo 'the file you want to upload is no picture, please go back to try again';

      exit;

      }

 

i guess you can change that to mp3, mp2, mp1, wav and ogg

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.