Jump to content

file extensions


grahamb314

Recommended Posts

Hi all,

 

I have an upload php page that uploads files to a directory.

 

What I need to do it make this more secure.

 

I want to limit the uploads to mp3's, wav's etc (all music files basically)

I do not want php files etc to be uploaded!

 

I also want a max file size of say 0.5mb to apply

 

How would you do this?

<?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? ////////////////////////
	}

}

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

}

}

?>

 

Thanks!

 

Link to comment
Share on other sites

I use this to check for images, and files under 500kbs... You'd have to check for what WAV/MP3s are to change that mime type.

 

   if ((($_FILES["file"]["type"] == "image/gif")
     || ($_FILES["file"]["type"] == "image/jpeg")
     || ($_FILES["file"]["type"] == "image/pjpeg"))
     && ($_FILES["file"]["size"] < 500000))
    {
    }

Link to comment
Share on other sites

I use this to check for images, and files under 500kbs... You'd have to check for what WAV/MP3s are to change that mime type.

 

   if ((($_FILES["file"]["type"] == "image/gif")
     || ($_FILES["file"]["type"] == "image/jpeg")
     || ($_FILES["file"]["type"] == "image/pjpeg"))
     && ($_FILES["file"]["size"] < 500000))
    {
    }

 

You can't trust the MIME type in the $_FILES array. Read the link I posted.

Link to comment
Share on other sites

I tried changing to verify, but the page goes blank. Any idea's why? The previous code would work, but obviously wouldn't verify the file type.

 

$file = $_FILES["file"]["tmp_name"];
$fi = new finfo(FILEINFO_MIME);
$mime_type = $fi->buffer(file_get_contents($file));
if ((($mime_type=="image/gif")
|| ($mime_type=="image/jpeg")
|| ($mime_type=="image/pjpeg"))
&& ($_FILES["file"]["size"] < 500000))
  { print "success";}

 

Not trying to hijack the thread, just curious. ;)

Link to comment
Share on other sites

People will not change the file extension to something else to do wrong doing, so soemthing simple should suffice!

 

Do you know every user personally, and is the page only accessible to them? Else you can never know..

 

But if you are sure; use something similar to Stryves' initial code. $file_array['type'] will hold the MIME type in your code, and the MIME type is 'audio/mpeg' for mp3, 'audio/ogg' for ogg and 'audio/wav', 'audio/wave' or 'audio/x-wav' for wave.

Link to comment
Share on other sites

I use this to check for images, and files under 500kbs... You'd have to check for what WAV/MP3s are to change that mime type.

 

   if ((($_FILES["file"]["type"] == "image/gif")
     || ($_FILES["file"]["type"] == "image/jpeg")
     || ($_FILES["file"]["type"] == "image/pjpeg"))
     && ($_FILES["file"]["size"] < 500000))
    {
    }

Where did you get the syntax

"image/gif"

from?

I need to equivilant ones for music files :-)

Thanks

 

 

Link to comment
Share on other sites

There's a pretty extensive list at W3Schools: http://www.w3schools.com/media/media_mimeref.asp

 

Or you can use Wikipedia to find the internet media types (originally called MIME types). On http://en.wikipedia.org/wiki/Ogg you can see the media types in the upper right box. "audio/ogg" would be for audio ogg's.

 

Else use Google, e.g. search for "{extension} mime type".

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.