Jump to content

MP3 Uploader


RaythMistwalker

Recommended Posts

Ok heres the deal:

 

ATM I am using SAM Broadcaster as an AutoDJ and LiveDJ. Due to me only holding certain songs on my computer i wish to allow users to add songs manually but only if they are in mp3.

 

Features

---

Upload the mp3 to directory

md5 the file name so there is no spaces

(IF POSSIBLE) get the ID3 info from the File for Artist and Title and save them in variables

^--- If thats not possible i will add Artist - Title to my form.

 

 

This has to work with ONLY mp3. All i really need is the php form for the upload- i can write the form easily myself.

 

Could someone possibly write a quick code or point me in direction of a tutorial as i have never done php uploading before.

 

Thanks in advance

~Rayth

Link to comment
Share on other sites

There are many tutorials and ready-made scripts to process file uploads.  A simple Google search will get you all you need. There are two methods to validate that a file is of the correct type. You can simply check the extension of the file to ensure it is ".mp3" (make sure to use strtolower()!) and/or you can also detect the file properties to ensure it is an mp3 (lest somebody simply renames the file extension to mp3). That would be done using finfo_file().

 

As for the ID3 MetaData you could use the class getID3(), which will read all ID3 tags for the file. http://getid3.sourceforge.net/

 

Link to comment
Share on other sites

Ok turns out i dunno howto do the form.

<?php

//**********************************************************************************************


echo "Please wait while we attempt to upload your file...<br><br>";

//**********************************************************************************************


$target_path = "autodj/";

$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.

$filename = $_FILES['uploadedfile']['name'];
$filesize = $_FILES['uploadedfile']['size'];
$mimetype = $_FILES['uploadedfile']['type'];

$filename = htmlentities($filename);
$filesize = htmlentities($filesize);
$mimetype = htmlentities($mimetype);

$target_path = $target_path . basename( $filename );

if($filename != ""){

echo "Beginning upload process for file named: ".$filename."<br>";
echo "Filesize: ".$filesize."<br>";
echo "Type: ".$mimetype."<br><br>";

}

//First generate a MD5 hash of what the new file name will be
//Force a MP3 extention on the file we are uploading

$hashedfilename = md5($filename);
$hashedfilename = $hashedfilename.".mp3";

//Check for empty file
if($filename == ""){
$error = "No File Exists!";
$flag = $flag + 1;

}

//Now we check that the file doesn't already exist.
$existname = "autodj/".$hashedfilename;

if(file_exists($existname)){

if($flag == 0){
$error = "Your file already exists on the server!  
Please choose another file to upload or rename the file on your
computer and try uploading it again!";
}

$flag = $flag + 1;
}

//Whitelisted files - Only allow files with MP3 extention onto server...

$whitelist = array(".mp3");
foreach ($whitelist as $ending) {

if(substr($filename, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!  
You can only upload MP3 files to the server!";
$flag++;
}
}


//Now we check the filesize.  If it is too big or too small then we reject it
//MP3 files should be at least 1MB and no more than 6.5 MB

if($filesize > 6920600){
//File is too large

if($flag == 0){
$error = "The file you are trying to upload is too large!  
Your file can be up to 6.5 MB in size only.  
Please upload a smaller MP3 file or encode your file with a lower bitrate.";
}

$flag = $flag + 1;
}

if($filesize < 1048600){
//File is too small

if($flag == 0){
$error = "The file you are trying to upload is too small!
Your file has been marked as suspicious because our system has
determined that it is too small to be a valid MP3 file.
Valid MP3 files must be bigger than 1 MB and smaller than 6.5 MB.";
}

$flag = $flag + 1;

}

//Check the mimetype of the file
if($mimetype != "audio/x-mp3" and $mimetype != "audio/mpeg"){

if($flag == 0){
$error = "The file you are trying to upload does not contain expected data.
Are you sure that the file is an MP3?";
}

$flag = $flag + 1;
}

//Check that the file really is an MP3 file by reading the first few characters of the file
$f = @fopen($_FILES['uploadedfile']['tmp_name'],'r');
$s = @fread($f,3);
@fclose($f);
if($s != "ID3"){

if($flag == 0){
$error = "The file you are attempting to upload does not appear to be a valid MP3 file.";
}

$flag++;
}



//All checks are done, actually move the file...

if($flag == 0){

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
   

    //Change the filename to MD5 hash and FORCE a MP3 extention.

    if(@file_exists("autodj/".$filename)){

    //Rename the file to an MD5 version
    rename("uploads/".$filename, "autodj/".$hashedfilename);

    echo "The file ".  basename( $filename ). "
      has been uploaded.  Your file is <a href='autodj/$hashedfilename'>here</a>.";
   
    }  
    else{
      echo "There was an error uploading the file, please try again!";
    }


} else{
    echo "There was an error uploading the file, please try again!";
}

}
else {
echo "File Upload Failed!<br>";
if($error != ""){
echo $error;
}
}

?>

 

What wuold i use for a user to select the file from their computer?

Link to comment
Share on other sites

What does the code you posted have to do with your question? I see no form in that code - where is that code?

 

When I typed in "form file upload" into Google, the second result was "PHP Tutorial - File Upload": http://www.tizag.com/phpT/fileupload.php

Wait, a tutorial on doing file uploads with PHP, sounds interesting. Maybe you should give that a read.

 

EDIT: I just completely rewrote this entire post. I had a lot of condescending and belittling statements. Although I think you may be worthy of such treatment I am trying to refrain from such commentary.

Link to comment
Share on other sites

Ok then heres the 2 codes im using:

 

upload.php (ie the form)

<form enctype="multipart/form-data" action="songupload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="6920600" />
Choose a mp3 file to upload: <input name="uploadedfile" type="file" /><br />
Enter Song Artist: <input name="songartist" type="text"><br>
Enter Song Title: <input name="songtitle" type="text"><br>
<input type="submit" value="Upload Song" />
</form>

 

songupload.php (uploader - found this one a website)

<?php

//**********************************************************************************************
ini_set('display_errors', 'on');
error_reporting(E_ALL);
require("config.php");
echo "Please wait while we attempt to upload your file...<br><br>";

//**********************************************************************************************
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}
$songartist = $clean($_POST['songartist']);
$songtitle = $clean($_POST['songtitle']);

$target_path = "autodj/";

$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.

$filename = $_FILES['uploadedfile']['name'];
$filesize = $_FILES['uploadedfile']['size'];
$mimetype = $_FILES['uploadedfile']['type'];

$filename = htmlentities($filename);
$filesize = htmlentities($filesize);
$mimetype = htmlentities($mimetype);

$target_path = $target_path . basename( $filename );

if($filename != ""){

echo "Beginning upload process for file named: ".$filename."<br>";
echo "Filesize: ".$filesize."<br>";
echo "Type: ".$mimetype."<br><br>";

}

//First generate a MD5 hash of what the new file name will be
//Force a MP3 extention on the file we are uploading

$hashedfilename = md5($filename);
$hashedfilename = $hashedfilename.".mp3";

//Check for empty file
if($filename == ""){
$error = "No File Exists!";
$flag = $flag + 1;

}

//Now we check that the file doesn't already exist.
$existname = "autodj/".$hashedfilename;

if(file_exists($existname)){

if($flag == 0){
$error = "Your file already exists on the server!  
Please choose another file to upload or rename the file on your
computer and try uploading it again!";
}

$flag = $flag + 1;
}

//Whitelisted files - Only allow files with MP3 extention onto server...

$whitelist = array(".mp3");
foreach ($whitelist as $ending) {

if(substr($filename, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!  
You can only upload MP3 files to the server!";
$flag++;
}
}


//Now we check the filesize.  If it is too big or too small then we reject it
//MP3 files should be at least 1MB and no more than 6.5 MB

if($filesize > 6920600){
//File is too large

if($flag == 0){
$error = "The file you are trying to upload is too large!  
Your file can be up to 6.5 MB in size only.  
Please upload a smaller MP3 file or encode your file with a lower bitrate.";
}

$flag = $flag + 1;
}

if($filesize < 1048600){
//File is too small

if($flag == 0){
$error = "The file you are trying to upload is too small!
Your file has been marked as suspicious because our system has
determined that it is too small to be a valid MP3 file.
Valid MP3 files must be bigger than 1 MB and smaller than 6.5 MB.";
}

$flag = $flag + 1;

}

//Check the mimetype of the file
if($mimetype != "audio/x-mp3" and $mimetype != "audio/mpeg"){

if($flag == 0){
$error = "The file you are trying to upload does not contain expected data.
Are you sure that the file is an MP3?";
}

$flag = $flag + 1;
}

//Check that the file really is an MP3 file by reading the first few characters of the file
$f = @fopen($_FILES['uploadedfile']['tmp_name'],'r');
$s = @fread($f,3);
@fclose($f);
if($s != "ID3"){

if($flag == 0){
$error = "The file you are attempting to upload does not appear to be a valid MP3 file.";
}

$flag++;
}



//All checks are done, actually move the file...

if($flag == 0){

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
   

    //Change the filename to MD5 hash and FORCE a MP3 extention.

    if(@file_exists("autodj/".$filename)){

    //Rename the file to an MD5 version
    rename("autodj/".$filename, "autodj/".$hashedfilename);
    $fulltitle = "http://radiolightning.co.cc/autodj/".$hashedfilename;

    echo "The file ".  basename( $filename ). "
      has been uploaded. .";
      $db = mysql_connect(DB_HOST,SAM_DB_USER,SAM_PASSWORD);
      if (!$db) { die('Could not connect to Database!'); }
      else {
          $addSong1 = "INSERT INTO `songlist`(filename, artist, title) VALUES('$fullurl','$songartist','$songtitle')";
          $sendquery = mysql_query($addSong1);
          if (!$sendquery) { die('Song not Added!'); }
          else {
              echo "Adding Song to Database for AutoDJ";
              $getSongId = "SELECT `ID` FROM `songlist` WHERE filename='$fullurl'";
              $RessongId = mysql_query($songId);
              if (!$RessongId) { die('Could not Retrieve Song Id'); }
              else {
                $songId = mysql_result($RessongId,ID);
                $catQuery = "INSERT INTO `categorylist`(songID,categoryID) VALUES('$songid','1')";
                $addCat = mysql_query($catQuery);
                if (!$addCat) { die('Failed to add Category'); }
                else { echo "Successfully added to Database"; }
              }
          }
      }
   
    }  
    else{
      echo "There was an error uploading the file, please try again!";
    }


} else{
    echo "There was an error uploading the file, please try again!";
}

}
else {
echo "File Upload Failed!<br>";
if($error != ""){
echo $error;
}
}

?>

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.