Jump to content

Recommended Posts

I'm working on a multi-file upload script and have two current problems.

1. The correct amount of upload boxes are not shown, there should be as many upload boxes as there are elements in the array $setOne

 

2. The files aren't uploading. Uploading worked fine when I was doing single uploads, what gives?

 

My code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>File Uploader</title>
<style type="text/css" title ="text/css" media="all">
.error {
	font-weight: bold;
	color: #C00
}
</style>
</head>
<body>
<?php #File uploading script
//start a loop to move through all the files
for($i = 0; $i < count($setOne); $i++){

//check to see if file has been submitted
if(isset($_POST['submitted'])){
	//if it has, check the type

	//debug - show all pertinate file info
	/*echo "<pre>";
        //print_r($_FILES);
        //echo "</pre>";
        */  
        
	//create an array of allowed types (NOTE: need MIME type for Flac)
	$allowed = array('audio/mpeg');
	//now evaluate current file
	//check to see if current file's type is in the array of allowed types
	if(in_array($_FILES['upload']['type'][$i], $allowed)){
		//If True, move the file to new location
		if(move_uploaded_file($_FILES['upload']['tmp_name'][$i], "/home/dan/MusicDataBase/{$_FILES['upload']['name'][$i]}")){
			echo '<p><em>The file has been uploaded!</em></p>';
		}//end of move_uploaded_file
	} else {//invaild type
		echo '<p class="error">Error: Wrong filetype. Must be mp3</p>';
	}//end else
}//end isset($_FILES['upload']

//now check for additional errors

if($FILES_['upload']['error'][$i] > 0){
	echo '<p class="error">The file could not be uploaded because: <strong>';
	//print message based on error
	switch($_FILES['upload']['error'][$i]){
		case 1:
			print 'The file exceeds the upload_max_filesize settiing in php.ini';
			break;
		case 2:
			print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
			break;
		case 3:
			print 'The file was only partially uploaded';
			break;
		case 4:
			print 'No file was uploaded';
			break;
		case 6:
			print 'No temporary folder was available';
			break;
		case 7:
			print 'Unable to write to the disk';
			break;
		case  8:
			print 'File upload was stopped';
			break;
		default:
			print 'A system error occured';
			break;
		}//end of switch

		print '</strong></p>';

	}//end of error if

	//Delete file if if still exist
	if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){
		unlink($_FILES['upload']['tmp_name'][$i]);
	}
}//end for loop

//////////////////////////////////////////////////////////////////
//the following are my functions which may or may not work
//their main function is to bring data from archiver.html
//and run various splitting and tagging functions on it
//aside  from the functions included here, everything
//works perfectly
//////////////////////////////////////////////////////////////////
//Initailize and assign values to starter variables.
    $show_id = "";
    $band = $_POST['band'];
    $date = $_POST['date'];
    $setOne = $_POST['setOne'];
    $set_oneNumOfSongs = count($setOne);
    
    ////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
      an array, and then makes it multidiminsional. It then creates and
       assigns song ids to the first row of the array and song titles
        to the third row */
    
    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);
    
    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
    	$dummy_array[$i] = trim($dummy_array[$i]);
}
    
    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));
    
    
    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
      $song_array[$i] = array(;
    }
    
    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
      $song_array[$m][2] = $dummy_array[$m];
    }
    
    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
      $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }
    
    //return newly created array
    return $song_array;
  }//create_song_array
  
function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
     It then produces an abbreviation from the band name.
     It then returns this abbreviation combined with the show date
     to create the show id*/
    
    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);
    
    //set abbreviation based on band name
    switch($band) {
      case "widespread panic" :
        $abb = "wsp";
        break;
      case "grateful dead" :
        $abb = "gd";
        break;
      case "phish" :
        $abb =  "ph";
        break;
      case "ryan adams" :
        $abb =  "ra";
        break;
      default:
        $abb = "unknown";
    }
    
    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
  }//end showIdCreator()  
    
    ////////////////end of functions//////////////////// 
    
    //create a unique show id
    $unique_show_id = showIdCreator($band, $date);

//create an array of songs based on set on and save it to an array of the same name
$set_one = create_song_array($setOne, $unique_show_id, "1");

//display show information
echo "The band is: " . $band . "<br>";
echo "The show date is: " . $date . "<br>";
echo "Unique show id: " . $unique_show_id . "<br><br>";
    				
?>

<form  enctype="multipart/form-data" action="upload_script.php" method="post">
<?php
//use a for loop to produce multiple upload boxes
for($i = 0; $i < count($setOne); $i++){
?>	
<input type ="hidden" name = "MAX_FILE_SIZE" value = "209715200" />

<fieldset><legend>Select a mp3 to upload:</legend>

<p><b>file:</b><input type="file" name ="upload[]" /></p>
</fieldset>

<?php
//end for loop
}
?>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

 

Any other comments on my code are greatly appreciated

Thanks,

Dan

Link to comment
https://forums.phpfreaks.com/topic/135610-solved-multi-file-upload-script/
Share on other sites

try this...

 

<?php
error_reporting(E_ALL); //always have error reporting on;

if(isset($_POST["uploaded"])){ //check for form submission;
      while(list($key,$value) = each($_FILES['upload']['name'])){
            if(!empty($value)){ //as long as $value is present, everything below this line is executed;
                  /*
                        assign your variables;
                  */
                  $tmpFileName = $_FILES['upload']['tmp_name'][$key]; //tmp filename;
                  $fileName = $_FILES['upload']['name'][$key]; //actual filename;
                  $uploadDir = "/home/dan/MusicDataBase"; //directory where file(s) will be uploaded;
                  
                  /*
                        get file extension;
                  */
                  $getExt = explode('.', $fileName);
                  $fileExt = $getExt[count($getExt)-1]; //got file extension;
                  $allowedExt = array("mp3","MP3"); //these are allowed extensions;
                  
                  if(in_array($fileExt, $allowedExt)){
                        if(is_uploaded_file($tmpFileName)){
                              $i = "ok"; //file extension is ok; file has been uploaded;
                        }else{
                              $errors[] = "Problem uploading file.";
                              $i = "invalid"; //extension is ok; file wasn't uploaded;
                        }
                  }else{
                        $errors[] = "Incorrect file extension.";
                        $i = "invalid"; //bad file extension;
                  }
                  switch($){
                        case ok:
                              //optional random filename generator .. uncomment below to use;
                              //$fileName = md5(time())."_".rand(0,999999999).".".$fileExt;
                              
                              @move_uploaded_file($tmpFileName,$uploadDir.'/'.$fileName); //move tmp file;                              
                        break;
                        
                        case invalid:
                              //display errors here;
                              if(is_array($errors)){
                                    $count = count($errors);
                                    foreach($errors as $k => $v){
                                          echo $v; //display errors;
                                          if($count>1){ echo '<br />'; } //add line break if multiple errors;
                                    }
                              }
                        break;
                  }
            }else{
                  //no file was uploaded;
            }
      }
}else{
      //form was not submitted;
}
?>

 

it's not tested, so try at own risk .. hope it helps!

Thanks so much mrMarcus! I didn't totally replace my script with yours because I didn't understand all of the coding. However I did include your first line

error_reporting(E_ALL); //always have error reporting on;

and after fixing a few variable inconsistencies I'm left with these two error messages:

 

Notice: Undefined index: upload in /opt/lampp/htdocs/MSNTesting/upload_script.php on line 52

 

Notice: Undefined index: upload in /opt/lampp/htdocs/MSNTesting/upload_script.php on line 87

 

any ideas on why I get these messages here and not on line 40?

All help is greatly appreciated.

I haven't changed much but, for reference, here's my newly revised code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>File Uploader</title>
<style type="text/css" title ="text/css" media="all">
.error {
	font-weight: bold;
	color: #C00
}
</style>
</head>
<body>
<?php #File uploading script

//turn on error reporting
error_reporting(E_ALL);
//start a loop to move through all the files
for($i = 0; $i < count($setOne); $i++){

//check to see if file has been submitted
if(isset($_POST['submitted'])){
	//if it has, check the type

	//debug - show all pertinate file info
	/*echo "<pre>";
        print_r($_FILES);
        echo "</pre>";
        */
        
	//create an array of allowed types (NOTE: need MIME type for Flac)
	$allowed = array('audio/mpeg');
	//now evaluate current file
	//check to see if current file's type is in the array of allowed types
	if(in_array($_FILES['upload']['type'][$i], $allowed)){
		//If True, move the file to new location
		if(move_uploaded_file($_FILES['upload']['tmp_name'][$i], "/home/dan/MusicDataBase/{$_FILES['upload']['name'][$i]}")){
			echo '<p><em>The file has been uploaded!</em></p>';
		}//end of move_uploaded_file
	} else {//invaild type
		echo '<p class="error">Error: Wrong filetype. Must be mp3</p>';
	}//end else
}//end isset($_FILES['upload']

//now check for additional errors

if($_FILES['upload']['error'][$i] > 0){
	echo '<p class="error">The file could not be uploaded because: <strong>';
	//print message based on error
	switch($_FILES['upload']['error'][$i]){
		case 1:
			print 'The file exceeds the upload_max_filesize settiing in php.ini';
			break;
		case 2:
			print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
			break;
		case 3:
			print 'The file was only partially uploaded';
			break;
		case 4:
			print 'No file was uploaded';
			break;
		case 6:
			print 'No temporary folder was available';
			break;
		case 7:
			print 'Unable to write to the disk';
			break;
		case  8:
			print 'File upload was stopped';
			break;
		default:
			print 'A system error occured';
			break;
		}//end of switch

		print '</strong></p>';

	}//end of error if

	//Delete file if if still exist
	if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){
		unlink($_FILES['upload']['tmp_name'][$i]);
	}
}//end for loop

//////////////////////////////////////////////////////////////////
//the following are my functions which may or may not work
//their main function is to bring data from archiver.html
//and run various splitting and tagging functions on it
//aside  from the functions included here, everything
//works perfectly
//////////////////////////////////////////////////////////////////
//Initailize and assign values to starter variables.
    $show_id = "";
    $band = $_POST['band'];
    $date = $_POST['date'];
    $setOne = $_POST['setOne'];
    $set_oneNumOfSongs = count($setOne);
    
    ////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
      an array, and then makes it multidiminsional. It then creates and
       assigns song ids to the first row of the array and song titles
        to the third row */
    
    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);
    
    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
    	$dummy_array[$i] = trim($dummy_array[$i]);
}
    
    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));
    
    
    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
      $song_array[$i] = array(;
    }
    
    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
      $song_array[$m][2] = $dummy_array[$m];
    }
    
    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
      $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }
    
    //return newly created array
    return $song_array;
  }//create_song_array
  
function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
     It then produces an abbreviation from the band name.
     It then returns this abbreviation combined with the show date
     to create the show id*/
    
    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);
    
    //set abbreviation based on band name
    switch($band) {
      case "widespread panic" :
        $abb = "wsp";
        break;
      case "grateful dead" :
        $abb = "gd";
        break;
      case "phish" :
        $abb =  "ph";
        break;
      case "ryan adams" :
        $abb =  "ra";
        break;
      default:
        $abb = "unknown";
    }
    
    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
  }//end showIdCreator()  
    
    ////////////////end of functions//////////////////// 
    
    //create a unique show id
    $unique_show_id = showIdCreator($band, $date);

//create an array of songs based on set on and save it to an array of the same name
$set_one = create_song_array($setOne, $unique_show_id, "1");

//display show information
echo "The band is: " . $band . "<br>";
echo "The show date is: " . $date . "<br>";
echo "Unique show id: " . $unique_show_id . "<br><br>";
    				
?>

<form  enctype="multipart/form-data" action="upload_script.php" method="post">
<?php
//use a for loop to produce multiple upload boxes
for($i = 0; $i < count($setOne); $i++){
?>	
<input type ="hidden" name = "MAX_FILE_SIZE" value = "209715200" />

<fieldset><legend>Select a mp3 to upload:</legend>

<p><b>file:</b><input type="file" name ="upload[]" /></p>
</fieldset>

<?php
//end for loop
}
?>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

 

Thanks, Dan

Alright gevans! That helped the script execute the correct number of times, but now I just get multiple instances of the same error,  :D. I'm still getting

Undefined index: upload

 

on the lines that read

if($_FILES['upload']['error'][$i] > 0){

and

if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){

 

I feel like I must be initializing the array incorrectly in the html or something...

 

Here's my updated code. I've reorganized it a little...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>File Uploader</title>
<style type="text/css" title ="text/css" media="all">
.error {
	font-weight: bold;
	color: #C00
}
</style>
</head>
<body>
<?php #File uploading script

//turn on error reporting
error_reporting(E_ALL);

//Initailize and assign values to starter variables.
    $show_id = "";
    $band = $_POST['band'];
    $date = $_POST['date'];
    $setOne = $_POST['setOne'];
    $set_oneNumOfSongs = count($setOne);
    
    //create a unique show id
    $unique_show_id = showIdCreator($band, $date);

//create an array of songs based on set on and save it to an array of the same name
$set_one = create_song_array($setOne, $unique_show_id, "1");
    
//start a loop to move through all the files
for($i = 0; $i < count($set_one); $i++){

//check to see if file has been submitted
if(isset($_POST['submitted'])){
	//if it has, check the type

	//debug - show all pertinate file info
	/*echo "<pre>";
        print_r($_FILES);
        echo "</pre>";
        */
        
	//create an array of allowed types (NOTE: need MIME type for Flac)
	$allowed = array('audio/mpeg');
	//now evaluate current file
	//check to see if current file's type is in the array of allowed types
	if(in_array($_FILES['upload']['type'][$i], $allowed)){
		//If True, move the file to new location
		if(move_uploaded_file($_FILES['upload']['tmp_name'][$i], "/home/dan/MusicDataBase/{$_FILES['upload']['name'][$i]}")){
			echo '<p><em>The file has been uploaded!</em></p>';
		}//end of move_uploaded_file
	} else {//invaild type
		echo '<p class="error">Error: Wrong filetype. Must be mp3</p>';
	}//end else
}//end isset($_FILES['upload']

//now check for additional errors

if($_FILES['upload']['error'][$i] > 0){
	echo '<p class="error">The file could not be uploaded because: <strong>';
	//print message based on error
	switch($_FILES['upload']['error'][$i]){
		case 1:
			print 'The file exceeds the upload_max_filesize settiing in php.ini';
			break;
		case 2:
			print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
			break;
		case 3:
			print 'The file was only partially uploaded';
			break;
		case 4:
			print 'No file was uploaded';
			break;
		case 6:
			print 'No temporary folder was available';
			break;
		case 7:
			print 'Unable to write to the disk';
			break;
		case  8:
			print 'File upload was stopped';
			break;
		default:
			print 'A system error occured';
			break;
		}//end of switch

		print '</strong></p>';

	}//end of error if

	//Delete file if if still exist
	if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){
		unlink($_FILES['upload']['tmp_name'][$i]);
	}
}//end for loop

//////////////////////////////////////////////////////////////////
//the following are my functions which may or may not work
//their main function is to bring data from archiver.html
//and run various splitting and tagging functions on it
//aside  from the functions included here, everything
//works perfectly
//////////////////////////////////////////////////////////////////

    ////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
      an array, and then makes it multidiminsional. It then creates and
       assigns song ids to the first row of the array and song titles
        to the third row */
    
    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);
    
    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
    	$dummy_array[$i] = trim($dummy_array[$i]);
}
    
    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));
    
    
    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
      $song_array[$i] = array(;
    }
    
    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
      $song_array[$m][2] = $dummy_array[$m];
    }
    
    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
      $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }
    
    //return newly created array
    return $song_array;
  }//create_song_array
  
function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
     It then produces an abbreviation from the band name.
     It then returns this abbreviation combined with the show date
     to create the show id*/
    
    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);
    
    //set abbreviation based on band name
    switch($band) {
      case "widespread panic" :
        $abb = "wsp";
        break;
      case "grateful dead" :
        $abb = "gd";
        break;
      case "phish" :
        $abb =  "ph";
        break;
      case "ryan adams" :
        $abb =  "ra";
        break;
      default:
        $abb = "unknown";
    }
    
    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
  }//end showIdCreator()  
    
    ////////////////end of functions//////////////////// 
    
    

//display show information
echo "The band is: " . $band . "<br>";
echo "The show date is: " . $date . "<br>";
echo "Unique show id: " . $unique_show_id . "<br><br>";
    				
?>

<form  enctype="multipart/form-data" action="upload_script.php" method="post">
<?php
//use a for loop to produce multiple upload boxes
for($i = 0; $i < count($set_one); $i++){
?>	
<input type ="hidden" name = "MAX_FILE_SIZE" value = "209715200" />

<fieldset><legend>Select a mp3 to upload:</legend>

<p><b>file:</b><input type="file" name ="upload[]" /></p>
</fieldset>

<?php
//end for loop
}
?>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

 

Thanks,

Dan

Do you see this in the code I have listed?

 //////////////////////////////////////////////////////////////////
   //the following are my functions which may or may not work
   //their main function is to bring data from archiver.html
   //and run various splitting and tagging functions on it
   //aside  from the functions included here, everything
   //works perfectly
   //////////////////////////////////////////////////////////////////
   
    ////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
      an array, and then makes it multidiminsional. It then creates and
       assigns song ids to the first row of the array and song titles
        to the third row */
    
    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);
    
    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
       $dummy_array[$i] = trim($dummy_array[$i]);
   }
    
    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));
    
    
    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
      $song_array[$i] = array(;
    }
    
    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
      $song_array[$m][2] = $dummy_array[$m];
    }
    
    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
      $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }
    
    //return newly created array
    return $song_array;
  }//create_song_array
  
function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
     It then produces an abbreviation from the band name.
     It then returns this abbreviation combined with the show date
     to create the show id*/
    
    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);
    
    //set abbreviation based on band name
    switch($band) {
      case "widespread panic" :
        $abb = "wsp";
        break;
      case "grateful dead" :
        $abb = "gd";
        break;
      case "phish" :
        $abb =  "ph";
        break;
      case "ryan adams" :
        $abb =  "ra";
        break;
      default:
        $abb = "unknown";
    }
    
    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
  }//end showIdCreator()  
    
    ////////////////end of functions//////////////////// 
    
    

   ?>

Alright gevans! That helped the script execute the correct number of times, but now I just get multiple instances of the same error,  :D. I'm still getting

Undefined index: upload

 

on the lines that read

if($_FILES['upload']['error'][$i] > 0){

and

if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){

 

I feel like I must be initializing the array incorrectly in the html or something...

 

Here's my updated code. I've reorganized it a little...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>File Uploader</title>
<style type="text/css" title ="text/css" media="all">
.error {
	font-weight: bold;
	color: #C00
}
</style>
</head>
<body>
<?php #File uploading script

//turn on error reporting
error_reporting(E_ALL);

//Initailize and assign values to starter variables.
    $show_id = "";
    $band = $_POST['band'];
    $date = $_POST['date'];
    $setOne = $_POST['setOne'];
    $set_oneNumOfSongs = count($setOne);
    
    //create a unique show id
    $unique_show_id = showIdCreator($band, $date);

//create an array of songs based on set on and save it to an array of the same name
$set_one = create_song_array($setOne, $unique_show_id, "1");
    
//start a loop to move through all the files
for($i = 0; $i < count($set_one); $i++){

//check to see if file has been submitted
if(isset($_POST['submitted'])){
	//if it has, check the type

	//debug - show all pertinate file info
	/*echo "<pre>";
        print_r($_FILES);
        echo "</pre>";
        */
        
	//create an array of allowed types (NOTE: need MIME type for Flac)
	$allowed = array('audio/mpeg');
	//now evaluate current file
	//check to see if current file's type is in the array of allowed types
	if(in_array($_FILES['upload']['type'][$i], $allowed)){
		//If True, move the file to new location
		if(move_uploaded_file($_FILES['upload']['tmp_name'][$i], "/home/dan/MusicDataBase/{$_FILES['upload']['name'][$i]}")){
			echo '<p><em>The file has been uploaded!</em></p>';
		}//end of move_uploaded_file
	} else {//invaild type
		echo '<p class="error">Error: Wrong filetype. Must be mp3</p>';
	}//end else
}//end isset($_FILES['upload']

//now check for additional errors

if($_FILES['upload']['error'][$i] > 0){
	echo '<p class="error">The file could not be uploaded because: <strong>';
	//print message based on error
	switch($_FILES['upload']['error'][$i]){
		case 1:
			print 'The file exceeds the upload_max_filesize settiing in php.ini';
			break;
		case 2:
			print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
			break;
		case 3:
			print 'The file was only partially uploaded';
			break;
		case 4:
			print 'No file was uploaded';
			break;
		case 6:
			print 'No temporary folder was available';
			break;
		case 7:
			print 'Unable to write to the disk';
			break;
		case  8:
			print 'File upload was stopped';
			break;
		default:
			print 'A system error occured';
			break;
		}//end of switch

		print '</strong></p>';

	}//end of error if

	//Delete file if if still exist
	if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){
		unlink($_FILES['upload']['tmp_name'][$i]);
	}
}//end for loop

//////////////////////////////////////////////////////////////////
//the following are my functions which may or may not work
//their main function is to bring data from archiver.html
//and run various splitting and tagging functions on it
//aside  from the functions included here, everything
//works perfectly
//////////////////////////////////////////////////////////////////

    ////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
      an array, and then makes it multidiminsional. It then creates and
       assigns song ids to the first row of the array and song titles
        to the third row */
    
    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);
    
    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
    	$dummy_array[$i] = trim($dummy_array[$i]);
}
    
    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));
    
    
    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
      $song_array[$i] = array(;
    }
    
    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
      $song_array[$m][2] = $dummy_array[$m];
    }
    
    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
      $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }
    
    //return newly created array
    return $song_array;
  }//create_song_array
  
function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
     It then produces an abbreviation from the band name.
     It then returns this abbreviation combined with the show date
     to create the show id*/
    
    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);
    
    //set abbreviation based on band name
    switch($band) {
      case "widespread panic" :
        $abb = "wsp";
        break;
      case "grateful dead" :
        $abb = "gd";
        break;
      case "phish" :
        $abb =  "ph";
        break;
      case "ryan adams" :
        $abb =  "ra";
        break;
      default:
        $abb = "unknown";
    }
    
    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
  }//end showIdCreator()  
    
    ////////////////end of functions//////////////////// 
    
    

//display show information
echo "The band is: " . $band . "<br>";
echo "The show date is: " . $date . "<br>";
echo "Unique show id: " . $unique_show_id . "<br><br>";
    				
?>

<form  enctype="multipart/form-data" action="upload_script.php" method="post">
<?php
//use a for loop to produce multiple upload boxes
for($i = 0; $i < count($set_one); $i++){
?>	
<input type ="hidden" name = "MAX_FILE_SIZE" value = "209715200" />

<fieldset><legend>Select a mp3 to upload:</legend>

<p><b>file:</b><input type="file" name ="upload[]" /></p>
</fieldset>

<?php
//end for loop
}
?>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

 

Thanks,

Dan

 

Gevans, the entire code is there.

The entire page is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>File Uploader</title>
<style type="text/css" title ="text/css" media="all">
.error {
	font-weight: bold;
	color: #C00
}
</style>
</head>
<body>
<?php #File uploading script

//turn on error reporting
error_reporting(E_ALL);

//Initailize and assign values to starter variables.
    $show_id = "";
    $band = $_POST['band'];
    $date = $_POST['date'];
    $setOne = $_POST['setOne'];
    
    
    //create a unique show id
    $unique_show_id = showIdCreator($band, $date);

//create an array of songs based on set on and save it to an array of the same name
$set_one = create_song_array($setOne, $unique_show_id, "1");
    
//start a loop to move through all the files
for($i = 0; $i < count($set_one); $i++){

//check to see if file has been submitted
if(isset($_POST['submitted'])){
	//if it has, check the type

	//debug - show all pertinate file info
	echo "<pre>";
        print_r($_FILES);
        echo "</pre>";
        
        
	//create an array of allowed types (NOTE: need MIME type for Flac)
	$allowed = array('audio/mpeg');
	//now evaluate current file
	//check to see if current file's type is in the array of allowed types
	if(in_array($_FILES['upload']['type'][$i], $allowed)){
		//If True, move the file to new location
		if(move_uploaded_file($_FILES['upload']['tmp_name'][$i], "/home/dan/MusicDataBase/{$_FILES['upload']['name'][$i]}")){
			echo '<p><em>The file has been uploaded!</em></p>';
		}//end of move_uploaded_file
	} else {//invaild type
		echo '<p class="error">Error: Wrong filetype. Must be mp3</p>';
	}//end else
}//end isset($_FILES['upload']

//now check for additional errors

if($_FILES['upload']['error'][$i] > 0){
	echo '<p class="error">The file could not be uploaded because: <strong>';
	//print message based on error
	switch($_FILES['upload']['error'][$i]){
		case 1:
			print 'The file exceeds the upload_max_filesize settiing in php.ini';
			break;
		case 2:
			print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
			break;
		case 3:
			print 'The file was only partially uploaded';
			break;
		case 4:
			print 'No file was uploaded';
			break;
		case 6:
			print 'No temporary folder was available';
			break;
		case 7:
			print 'Unable to write to the disk';
			break;
		case  8:
			print 'File upload was stopped';
			break;
		default:
			print 'A system error occured';
			break;
		}//end of switch

		print '</strong></p>';

	}//end of error if

	//Delete file if if still exist
	if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){
		unlink($_FILES['upload']['tmp_name'][$i]);
	}
}//end for loop

//////////////////////////////////////////////////////////////////
//the following are my functions which may or may not work
//their main function is to bring data from archiver.html
//and run various splitting and tagging functions on it
//aside  from the functions included here, everything
//works perfectly
//////////////////////////////////////////////////////////////////

    ////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
      an array, and then makes it multidiminsional. It then creates and
       assigns song ids to the first row of the array and song titles
        to the third row */
    
    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);
    
    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
    	$dummy_array[$i] = trim($dummy_array[$i]);
}
    
    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));
    
    
    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
      $song_array[$i] = array(;
    }
    
    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
      $song_array[$m][2] = $dummy_array[$m];
    }
    
    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
      $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }
    
    //return newly created array
    return $song_array;
  }//create_song_array
  
function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
     It then produces an abbreviation from the band name.
     It then returns this abbreviation combined with the show date
     to create the show id*/
    
    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);
    
    //set abbreviation based on band name
    switch($band) {
      case "widespread panic" :
        $abb = "wsp";
        break;
      case "grateful dead" :
        $abb = "gd";
        break;
      case "phish" :
        $abb =  "ph";
        break;
      case "ryan adams" :
        $abb =  "ra";
        break;
      default:
        $abb = "unknown";
    }
    
    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
  }//end showIdCreator()  
    
    ////////////////end of functions//////////////////// 
    
    

//display show information
echo "The band is: " . $band . "<br>";
echo "The show date is: " . $date . "<br>";
echo "Unique show id: " . $unique_show_id . "<br><br>";
    				
?>

<form  enctype="multipart/form-data" action="upload_script.php" method="post">
<?php
//use a for loop to produce multiple upload boxes
for($i = 0; $i < count($set_one); $i++){
?>	
<input type ="hidden" name = "MAX_FILE_SIZE" value = "209715200" />

<fieldset><legend>Select a mp3 to upload:</legend>

<p><b>file:</b><input type="file" name ="upload[]" /></p>
</fieldset>

<?php
//end for loop
}
?>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

Ok I've had a very big fiddle,

 

keep a copy of your old code coz I can't test this right now;

 

<?php #File uploading script

//////////////////////////////////////////////////////////////////
//the following are my functions which may or may not work
//their main function is to bring data from archiver.html
//and run various splitting and tagging functions on it
//aside  from the functions included here, everything
//works perfectly
//////////////////////////////////////////////////////////////////

////////////////functions////////////////////
function create_song_array($set, $unique_show_id, $set_num) {
    /*function takes in show.set object, splits that object into
    an array, and then makes it multidiminsional. It then creates and
    assigns song ids to the first row of the array and song titles
    to the third row */

    //split the object into a dummy array based on ',' and '>', using perl-type regex
    $dummy_array = preg_split("/(,|>)/", $set);

    //use the trim() function to remove leading/trailing white spaces
    for($i=0;$i<count($dummy_array);$i++){
        $dummy_array[$i] = trim($dummy_array[$i]);
    }

    //then create the real array as a copy of dummy array
    $song_array = array(count($dummy_array));


    //make the real array multidiminsional and create eight
    //rows to hold the song's various properties
    for($i = 0; $i < count($dummy_array); $i++){
        $song_array[$i] = array(;
    }

    //assign song title, set_one_songs[2] = dummy_array[1]
    for($m = 0; $m < count($dummy_array); $m++){
        $song_array[$m][2] = $dummy_array[$m];
    }

    //assign unique song ids
    for($n = 0; $n < count($dummy_array); $n++) {
        $song_array[$n][0] = $unique_show_id . "s" . $set_num . "s" . ($n+1);
    }

    //return newly created array
    return $song_array;
}//create_song_array

function showIdCreator($band, $date) {
    /*this function takes in the user inputed band name and show date.
    It then produces an abbreviation from the band name.
    It then returns this abbreviation combined with the show date
    to create the show id*/

    //variable declarations
    $abb;//store the abbreviated band name
    //convert the band name to lower case for easier comparison
    $band = strtolower($band);

    //set abbreviation based on band name
    switch($band) {
        case "widespread panic" :
            $abb = "wsp";
            break;
        case "grateful dead" :
            $abb = "gd";
            break;
        case "phish" :
            $abb =  "ph";
            break;
        case "ryan adams" :
            $abb =  "ra";
            break;
        default:
        $abb = "unknown";
    }

    //return a combination of the band name abbreviation and the show date
    return $abb . $date;
}//end showIdCreator()  

////////////////end of functions//////////////////// 

function printHead(){
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
            <html xmlns="http://www.w3.org/1999/xhtml"
            xml:lang="en" lang="en">

            <head>
       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
       
       <title>File Uploader</title>
       <style type="text/css" title ="text/css" media="all">
       .error {
          font-weight: bold;
          color: #C00
       }
       </style>
       </head>
       <body>';
}
   
//turn on error reporting
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

printHead(); #print html head

//Initailize and assign values to starter variables.
$show_id = "";
$band = $_POST['band'];
$date = $_POST['date'];
$setOne = $_POST['setOne'];

//create a unique show id
$unique_show_id = showIdCreator($band, $date);

//create an array of songs based on set on and save it to an array of the same name
$set_one = create_song_array($setOne, $unique_show_id, "1");

//check to see if file has been submitted
if(isset($_POST['submitted'])){
    //start a loop to move through all the files
    for($i = 0; $i < count($set_one); $i++){
        //if it has, check the type

        //debug - show all pertinate file info
        /*echo "<pre>";
        print_r($_FILES);
        echo "</pre>";*/

        //create an array of allowed types (NOTE: need MIME type for Flac)
        $allowed = array('audio/mpeg');
        
        //now evaluate current file
        //check to see if current file's type is in the array of allowed types
        if(in_array($_FILES['upload']['type'][$i], $allowed)){
            //If True, move the file to new location
            if(move_uploaded_file($_FILES['upload']['tmp_name'][$i], "/home/dan/MusicDataBase/{$_FILES['upload']['name'][$i]}")){
                echo '<p><em>The file has been uploaded!</em></p>';
                //Delete file if if still exist
                if(file_exists($_FILES['upload']['tmp_name'][$i]) && is_files($_FILES['upload']['tmp_name'][$i])){
                    unlink($_FILES['upload']['tmp_name'][$i]);
                }
            } else {
                if($_FILES['upload']['error'][$i] > 0){
                    echo '<p class="error">The file could not be uploaded because: <strong>';
                    //print message based on error
                    switch($_FILES['upload']['error'][$i]){
                        case 1:
                            print 'The file exceeds the upload_max_filesize settiing in php.ini';
                            break;
                        case 2:
                            print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
                            break;
                        case 3:
                            print 'The file was only partially uploaded';
                            break;
                        case 4:
                            print 'No file was uploaded';
                            break;
                        case 6:
                            print 'No temporary folder was available';
                            break;
                        case 7:
                            print 'Unable to write to the disk';
                            break;
                        case  8:
                            print 'File upload was stopped';
                            break;
                        default:
                            print 'A system error occured';
                            break;
                    }//end of switch
                    print '</strong></p>';
                }//end of error if
            }
        } else {//invaild type
            echo '<p class="error">Error: Wrong filetype. Must be mp3</p>';
        }//end else
    }
}//end for loop


//display show information
echo "The band is: " . $band . "<br>";
echo "The show date is: " . $date . "<br>";
echo "Unique show id: " . $unique_show_id . "<br><br>";

?>

<form  enctype="multipart/form-data" action="upload_script.php" method="post">
<?php
//use a for loop to produce multiple upload boxes
for($i = 0; $i < count($set_one); $i++){
   ?>   
<input type ="hidden" name = "MAX_FILE_SIZE" value = "209715200" />

<fieldset><legend>Select a mp3 to upload:</legend>

<p><b>file:</b><input type="file" name ="upload[]" /></p>
</fieldset>

<?php
//end for loop
}
?>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>

That's what I thought. I notice some code switches back and forth between the two and was wondering why. I suppose it's just preference...

 

Yea, just preference, whoever echo is proven to be faster than print. Since I bench tested that and found that out I only use echo and tend to use ' over " for a similiar reason. Although it does not speed it up noticeably I figure anything helps =)

Thanks for the link gevans. Could you tell me what you meant to change with the new code you posted? Without doing a line-by-line comparison, it looks pretty similar to the code I was already working with (save the header being in it's own function). Also, is there a functional reason why the header is in it's own function, or is that just a matter of style/tidyness?

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.