View Full Version : Test file type uploaded
MatKieng
03-15-2004, 10:48 AM
Dear,
I'm creating a website with upload file functionality. I want only mp3 and wav file are allowed to upload. Can you provide me an example? Thanks for reading.
Regards.
In the upload script, use an array of allowed file types to compare against the type defined in $_FILES['file']['type'] like so:
<?php
// check to see if form was submitted
if ( isset($_POST['upload']) )
{
// define some variables to use
$filename = $_FILES['file']['name'];
$file_tmp_name = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];
$uploads_dir = './uploads/';
// these are the file types we want to allow
$allowed_types = array('audio/mpeg', 'audio/wav');
// check to see if the file type matches
if ( !in_array($file_type, $allowed_types) )
{
echo '
File type not allowed!</p>';
}
else
{
// file type is apparently ok, try to move the file into the directory
if ( move_uploaded_file($file_tmp_name, $uploads_dir . $filename) )
{
echo '
File '.$filename.' was uploaded successfully!</p>';
}
}
}
?>
MatKieng
03-16-2004, 03:16 AM
Thanks bdl for answering me!
DeadlySin3
03-19-2004, 03:15 AM
I've written two versions of an upload script, both deal mainly with images, but with little modification, they could be used to accept .wav and .mp3 files only.
http://www.deadlysin3.net/FREE/uploadScript/
This should give ya a pretty good idea for a nice upload script
DeadlySin3>
Some constructive criticism on the file upload scripts -
It's outdated. ($HTTP_POST_FILES)
A foreach() loop within a foreach() loop? Do you have time to brew a cup of coffee while the script runs?
Speaking of the foreach() loop, it assumes multiple files, but the form doesn't provide for multiple files, so it's a moot point.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.