3 Replies - 572 Views - Last Post: 09 October 2011 - 08:17 AM Rate Topic: -----

#1 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Uploading multiple files not working?

Posted 08 October 2011 - 09:49 PM

My script to upload audio files is not working when uploading certain mp3 files?

<?php

$target_path = "uploads/music/";

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

$filename1 = $_FILES['uploadedfile1']['name'];
$mimetype1 = $_FILES['uploadedfile1']['type'];
//**********************************************************************************************
$filename2 = $_FILES['uploadedfile2']['name'];
$mimetype2 = $_FILES['uploadedfile2']['type'];
//**********************************************************************************************
$filename3 = $_FILES['uploadedfile3']['name'];
$mimetype3 = $_FILES['uploadedfile3']['type'];


$filename1 = htmlentities($filename1);
$mimetype1 = htmlentities($mimetype1);
//**********************************************************************************************
$filename2 = htmlentities($filename2);
$mimetype2 = htmlentities($mimetype2);
//**********************************************************************************************
$filename3 = htmlentities($filename3);
$mimetype3 = htmlentities($mimetype3);


$target_path1 = $target_path . basename( $filename1 );
//**********************************************************************************************
$target_path2 = $target_path . basename( $filename2 );
//**********************************************************************************************
$target_path3 = $target_path . basename( $filename3 );



if($filename1 != ""){

echo "".$filename1."<br>";
echo "Filesize: ".$filesize1."<br>";
echo "Type: ".$mimetype1."<br><br>";

}
//**********************************************************************************************
if($filename2 != ""){

echo "".$filename2."<br>";
echo "Filesize: ".$filesize2."<br>";
echo "Type: ".$mimetype2."<br><br>";

}
//**********************************************************************************************
if($filename3 != ""){

echo "".$filename3."<br>";
echo "Filesize: ".$filesize3."<br>";
echo "Type: ".$mimetype3."<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

$hashedfilename1 = md5($filename1);
$hashedfilename1 = $hashedfilename1.".mp3";
//**********************************************************************************************
$hashedfilename2 = md5($filename2);
$hashedfilename2 = $hashedfilename2.".mp3";
//**********************************************************************************************
$hashedfilename3 = md5($filename3);
$hashedfilename3 = $hashedfilename3.".mp3";





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

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

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

}



//Now we check that the file doesn't already exist.
$existname1 = "uploads/music/".$hashedfilename1;
//**********************************************************************************************
//Now we check that the file doesn't already exist.
$existname2 = "uploads/music/".$hashedfilename2;
//**********************************************************************************************
//Now we check that the file doesn't already exist.
$existname3 = "uploads/music/".$hashedfilename3;





if(file_exists($existname1)){

if($flag == 0){
$error = "You have already uploaded this file!";
}

$flag = $flag + 1;
}
//**********************************************************************************************
if(file_exists($existname2)){

if($flag == 0){
$error = "You have already uploaded this file!";
}

$flag = $flag + 1;
}
//**********************************************************************************************
if(file_exists($existname3)){

if($flag == 0){
$error = "You have already uploaded this file!";
}

$flag = $flag + 1;
}




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

$whitelist1 = array(".mp3", "mpeg");
foreach ($whitelist1 as $ending) {

if(substr($filename1, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!  
You can only upload MP3 files to our server!";
$flag++;
}
}
//**********************************************************************************************
$whitelist2 = array(".mp3", "mpeg");
foreach ($whitelist2 as $ending) {

if(substr($filename2, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!  
You can only upload MP3 files to our server!";
$flag++;
}
}
//**********************************************************************************************
$whitelist3 = array(".mp3", "mpeg");
foreach ($whitelist3 as $ending) {

if(substr($filename3, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!  
You can only upload MP3 files to our 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($filesize1 > 13631488){
//File is too large

if($flag == 0){
$error = "The file you are trying to upload is too large!";
}

$flag = $flag + 1;
}
//**********************************************************************************************
if($filesize2 > 13631488){
//File is too large

if($flag == 0){
$error = "The file you are trying to upload is too large!";
}

$flag = $flag + 1;
}
//**********************************************************************************************
if($filesize3 > 13631488){
//File is too large

if($flag == 0){
$error = "The file you are trying to upload is too large!";
}

$flag = $flag + 1;
}



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

if($flag == 0){
$error = "The file you are trying to upload is too small!";
}

$flag = $flag + 1;

}
//**********************************************************************************************
if($filesize2 < 1048600){
//File is too small

if($flag == 0){
$error = "The file you are trying to upload is too small!";
}

$flag = $flag + 1;

}
//**********************************************************************************************
if($filesize3 < 1048600){
//File is too small

if($flag == 0){
$error = "The file you are trying to upload is too small!";
}

$flag = $flag + 1;

}



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

if($flag == 0){
$error = "Only MP3 files are allowed!";
}

$flag = $flag + 1;
}
//**********************************************************************************************
if($mimetype2 != "audio/x-mp3" and $mimetype2 != "audio/mpeg"){

if($flag == 0){
$error = "Only MP3 files are allowed!";
}

$flag = $flag + 1;
}
//**********************************************************************************************
if($mimetype3 != "audio/x-mp3" and $mimetype3 != "audio/mpeg"){

if($flag == 0){
$error = "Only MP3 files are allowed!";
}

$flag = $flag + 1;
}



//Check that the file really is an MP3 file by reading the first few characters of the file
$f = @fopen($_FILES['uploadedfile1']['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++;
}
//**********************************************************************************************
$f = @fopen($_FILES['uploadedfile2']['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++;
}
//**********************************************************************************************
$f = @fopen($_FILES['uploadedfile3']['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['uploadedfile1']['tmp_name'], $target_path1)) {
  

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

   if(@file_exists("uploads/music/".$filename1)){

   //Rename the file to an MD5 version
   rename("uploads/music/".$filename1, "uploads/music/".$hashedfilename1);

   echo "The file ".  basename( $filename1 ). "
     has been uploaded.  Your file is <a href='uploads/music/$hashedfilename1'>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;
}
}
//**********************************************************************************************
if($flag == 0){

if(move_uploaded_file($_FILES['uploadedfile2']['tmp_name'], $target_path2)) {
  

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

   if(@file_exists("uploads/music/".$filename2)){

   //Rename the file to an MD5 version
   rename("uploads/music/".$filename2, "uploads/music/".$hashedfilename2);

   echo "The file ".  basename( $filename2 ). "
     has been uploaded.  Your file is <a href='uploads/music/$hashedfilename2'>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;
}
}
//**********************************************************************************************
if($flag == 0){

if(move_uploaded_file($_FILES['uploadedfile3']['tmp_name'], $target_path3)) {
  

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

   if(@file_exists("uploads/music/".$filename3)){

   //Rename the file to an MD5 version
   rename("uploads/music/".$filename3, "uploads/music/".$hashedfilename3);

   echo "The file ".  basename( $filename3 ). "
     has been uploaded.  Your file is <a href='uploads/music/$hashedfilename3'>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;
}
}



If anyone can help me out that would be awesome thank you!

This post has been edited by dbeaird727: 08 October 2011 - 09:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Uploading multiple files not working?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,088
  • Joined: 27-December 08

Re: Uploading multiple files not working?

Posted 08 October 2011 - 09:51 PM

Define "doesn't work." Also, didn't we go over this already?
http://www.dreaminco...1&#entry1442605
http://www.dreaminco...1&#entry1439050
Was This Post Helpful? 0
  • +
  • -

#3 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Uploading multiple files not working?

Posted 08 October 2011 - 09:55 PM

No I dont think I have posted all of the code for this script prior to this post and it uploads the hashed filename into the database but does not insert it into the server.. So it is very close to working but doesnt work completely.
Was This Post Helpful? 0
  • +
  • -

#4 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Uploading multiple files not working?

Posted 09 October 2011 - 08:17 AM

Any ideas as to why.. I know my php.ini allows up to 32MB to be uploaded at once and the file size allowed in the script is around 12MB.. I know the files i was uploading were under 10MB.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1