Jump to content

Music Uploader


rik72

Recommended Posts

Hey,

 

Please don't be put off by the title of this post, it's for unsigned bands to post their music so that they can be played on an online radio. I have a file uploader script currently, but i need a way of renaming the file and making sure it doesn't already exist.

 

It would also be useful to have a size limiter at 15mb.

 

I will make a donation via paypal to the completer.

 

any help is appreciated, here is the code i'm currently using,

 

<?php
$uploaddir = "upload/"; //Upload directory: needs write premissions
$log = "uploadlog.txt"; // Upload LOG file
// what file types do you want to disallow?
$blacklist = array(".php", ".phtml", ".php3", ".php4", ".php5", ".exe", ".js",".html", ".htm", ".inc");
// allowed filetypes       
$allowed_filetypes = array('.mp3','.wma','.wav','.mpeg'); 
    
if (!is_dir($uploaddir)) {
    die ("Upload directory does not exists.");
}
if (!is_writable($uploaddir)) {
    die ("Upload directory is not writable.");
}

if ($_POST['cmdupload'])
{
    
$ip = trim($_SERVER['REMOTE_ADDR']); 

    if (isset($_FILES['file']))
    {
        if ($_FILES['file']['error'] != 0)
        {
            switch ($_FILES['file']['error'])
            {
                case 1:
                    print 'The file is to big.'; // php installation max file size error
                    exit;
                    break;
                case 2:
                    print 'The file is to big.'; // form max file size error
                    exit;
                    break;
                case 3:
                    print 'Only part of the file was uploaded';
                    exit;
                    break;
                case 4:
                    print 'No file was uploaded</p>';
                    exit;
                    break;
                case 6:
                    print "Missing a temporary folder.";
                    exit;
                    break;
                case 7:
                    print "Failed to write file to disk";
                    exit;
                    break;
                case 8:
                    print "File upload stopped by extension";
                    exit;
                    break;

            }
        } else {
            foreach ($blacklist as $item)
            {
                if (preg_match("/$item\$/i", $_FILES['file']['name']))
                {
                    echo "Invalid filetype !";
                        $date = date("m/d/Y"); 
                        $time = date("h:i:s A");                 
                        $fp = fopen($log,"ab"); 
                        fwrite($fp,"$ip | ".$_FILES['file']['name']." | $date | $time | INVALID TYPE"."\r\n"); 
                        fclose($fp);
                    unset($_FILES['file']['tmp_name']);
                    exit;
                }
            }
            // Get the extension from the filename.
            $ext = substr($_FILES['file']['name'], strpos($_FILES['file']['name'],'.'), strlen($_FILES['file']['name'])-1); 
      // Check if the filetype is allowed, if not DIE and inform the user.
      if(!in_array($ext,$allowed_filetypes)){
                        $date = date("m/d/Y"); 
                        $time = date("h:i:s A");                     
                        $fp = fopen($log,"ab"); 
                        fwrite($fp,"$ip | ".$_FILES['file']['name']." | $date | $time | INVALID TYPE"."\r\n"); 
                        fclose($fp);
                die('The file you attempted to upload is not allowed.');
            }
            if (!file_exists($uploaddir . $_FILES["file"]["name"]))
            {
                // Proceed with file upload
                if (is_uploaded_file($_FILES['file']['tmp_name']))
                {
                    //File was uploaded to the temp dir, continue upload process
                    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name']))
                    {
                        // uploaded file was moved and renamed succesfuly. Display a message.
                        echo "Upload successful !";
                        // Now log the uploaders IP adress date and time
                        $date = date("m/d/Y"); 
                        $time = date("h:i:s A");                
                        $fp = fopen($log,"ab"); 
                        fwrite($fp,"$ip | ".$_FILES['file']['name']." | $date | $time | OK"."\r\n"); 
                        fclose($fp); 
                    } else {
                        echo "Error while uploading the file, Please contact the webmaster.";
                        unset($_FILES['file']['tmp_name']);
                    }
                } else {
                    //File was NOT uploaded to the temp dir
                    switch ($_FILES['file']['error'])
                    {
                        case 1:
                            print 'The file is to big.'; // php installation max file size error
                            break;
                        case 2:
                            print 'The file is to big.'; // form max file size error
                            break;
                        case 3:
                            print 'Only part of the file was uploaded';
                            break;
                        case 4:
                            print 'No file was uploaded</p>';
                            break;
                        case 6:
                            print "Missing a temporary folder.";
                            break;
                        case 7:
                            print "Failed to write file to disk";
                            break;
                        case 8:
                            print "File upload stopped by extension";
                            break;

                    }

                }
            } else {
                echo "Filename already exists, Please rename the file and retry.";
                unset($_FILES['file']['tmp_name']);
            }
        }
    } else {
        // user did not select a file to upload
        echo "Please select a file to upload.";       
    }
} else {
    // upload button was not pressed
    header("Location: uploadform.php");
}
?>

Link to comment
Share on other sites

also, instead of using $_FILES['file']['tmp_name'] and $_FILES['file']['name']  declare them at the beggining and make them smaller

e.g: $tmpname = $_FILES['file']['tmp_name'];

and

$name = $_FILES['file']['name'];

 

not really important, but i thought it was worth a mention

 

maybe try this when the file has been uploaded to the temp directory

 

<?php
$tmpname = $_FILES['file']['tmp_name'];

$name = $_FILES['file']['name'];

$rand = rand(1, 99999);

$name = $name .'_'. $rand .'.'. $ext;

move_uploaded_file($tmpname, $name);

?>

 

i havent made one of these in a while but that appends a random 6 digit number to the end of the filename when you move it.

 

worth a try?

Link to comment
Share on other sites

<?php
$filename = $_FILES['file']['name'];
$dotPos = strpos($file, '.');
$newname = substr($filename, 0, $dotPos) . '-' . rand(10000, 99999) . $ext;
$ext = strtolower(substr(strrchr($filename, '.'), 1));
$size = $_FILES['file']['size'];
$allowedExt = array('.mp3','.wma','.wav','.mpeg');
$maxSize = 15000000; //very approximate 15MB
if(in_array($ext, $allowedExt)){
     if($size < $maxSize){
          if(move_uploaded_file($_FILES['file']['tmp_name'], $newname)){
                echo 'The file was successfuly uploaded';
          } else{
                echo 'An error occurred while uploading. Please try again.';
          }
     } else{
          echo 'Filesize is too big';
     }
} else{
    echo 'Filetype not supported.';
}
?>

 

As for the 15 MB file limit, u should configure php (and web server i guess) to increase the post limit, execution time and stuff. Also consider that the uploaders should have a fast internet connection to upload 15 MB without their browsers timing out.

Link to comment
Share on other sites

If you just append a random 6 digits there is a small chance that a file will already exist with the same name. Either check the file doesn't exist before you try and move it, or (better) append something which will definitely not be repeated - such as a unix timestamp.

Link to comment
Share on other sites

If you just append a random 6 digits there is a small chance that a file will already exist with the same name. Either check the file doesn't exist before you try and move it, or (better) append something which will definitely not be repeated - such as a unix timestamp.

 

yeah a timestamp would be a good idea. although the chances of the number being 1 in 999999, or however many digits you decide to go up to, there is always that chance.  but a timstamp is a good idea.

Link to comment
Share on other sites

I tried this, as suggested

 

<?php
$filename = $_FILES['file']['name'];
$dotPos = strpos($file, '.');
$newname = substr($filename, 0, $dotPos) . '-' . rand(10000, 99999) . $ext;
$ext = strtolower(substr(strrchr($filename, '.'), 1));
$size = $_FILES['file']['size'];
$allowedExt = array('.mp3','.wma','.wav','.mpeg');
$maxSize = 15000000; //very approximate 15MB
if(in_array($ext, $allowedExt)){
     if($size < $maxSize){
          if(move_uploaded_file($_FILES['file']['tmp_name'], $newname)){
                echo 'The file was successfuly uploaded';
          } else{
                echo 'An error occurred while uploading. Please try again.';
          }
     } else{
          echo 'Filesize is too big';
     }
} else{
    echo 'Filetype not supported.';
}
?>

 

Made an uploader page, but keep getting "Filetype not supported." on mp3's, any ideas? Also, I really need it to upload it to another directory, like /songs and do a check before uploading..

 

 

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.