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;
	}

}

}

?>

Link to comment
Share on other sites

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

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.