Restricting file type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dakkonz
Forum Commoner
Posts: 69
Joined: Sat Dec 27, 2003 2:55 am
Location: Asia

Restricting file type

Post by dakkonz »

I would like to know if there is a way to restrict the file types uploaded into the server....
I wan to restrict it to mp3 and midi.... but so far the internet only give me images
thanks
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Can't you just check the extension of the uploaded file using stristr() ?

Code: Select all

<?php

$isMP3 = stristr($theFileName, ".mp3");

if($isMP3 !== false)
{
     // The file is an MP3 so do something here
}

?>
I think there's also a way of checking the MIME type of a file.. but I can't remember how to do it at the moment. If I remember I will let you know.

Another way would be to use JavaScript to check the file extension before the user even uploads it to the server. onsubmit="" would be a good starting point.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Mime types for MP3's and MIDI's:

Code: Select all

.mp3    audio/mpeg3
.mp3 	audio/x-mpeg-3
.mp3 	video/mpeg
.mp3 	video/x-mpeg
.mid    audio/midi
.mid 	audio/x-mid
.mid 	audio/x-midi
.mid 	music/crescendo
.mid 	x-music/x-midi
.midi   application/x-midi
.midi   audio/midi
.midi   audio/x-mid
.midi   audio/x-midi
.midi   music/crescendo
.midi   x-music/x-midi
One way would be to compare the $_FILES['userfile']['type'] with an array of all these mimetypes (where 'userfile' is the name of the form input. Example: <input type="file" name="userfile">).

More info on uploading:
http://www.php.net/manual/en/features.file-upload.php
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Gen-ik wrote:Can't you just check the extension of the uploaded file using stristr() ?

Code: Select all

$theFileName = 'Bad.mp3.FileName.exe';
Just commenting; using strstr is not recommended, as example above shows. Either substr() and/or explode() (using . and the last result in the array) would be better.

Code: Select all

$theFileName = 'Bad.mp3.FileName.bat';
$temp = explode('.',$theFileName);
echo $temp[count($temp)-1];
// bat
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

JAM wrote:
Gen-ik wrote:Can't you just check the extension of the uploaded file using stristr() ?

Code: Select all

$theFileName = 'Bad.mp3.FileName.exe';
Just commenting; using strstr is not recommended, as example above shows. Either substr() and/or explode() (using . and the last result in the array) would be better.

Code: Select all

$theFileName = 'Bad.mp3.FileName.bat';
$temp = explode('.',$theFileName);
echo $temp[count($temp)-1];
// bat
Yeah that's a good point actually... should of thought about it myself really. :oops:
Post Reply