I'm trying to write a script where users can upload as many as 12 images to my server. The end result of this will be a very simple gallery. I have run into a problem. I am storing the information in an array, but whenever all the upload forms are not used, information in still stored in the array. Is there a way to tell the array to disregard the fields that are not filled out?? Perhaps some code will make this more clear:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="multipleupload.php" method="post"  enctype="multipart/form-data">
<input name="test[]" type="file" id="test[]">
<input name="test[]" type="file" id="test[]">
<input name="test[]" type="file" id="test[]">
<input type="submit" value="Upload!">
</form>
</body>
</html>
<?
require 'dbcon.php';

$images=serialize($_FILES['test']);
$count=count($_FILES['test']['name']);
$i=0;
echo " This is the $count";

// ==============
// Configuration
// ==============
$allowed_ext = "jpg"; // These are the allowed extensions of the files that are uploaded
$max_size = "50000"; // 50000 is the same as 50kb
$max_height = "600"; // This is in pixels
$max_width = "900"; // This is in pixels

$uploaddir = "/home/atlpartylife/atlpartylife.com/test/uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
	while($i <= $count) {
						// Check Height & Width
						if ($max_width && $max_height) {
							list($width, $height, $type, $w) = getimagesize($_FILES['test']['tmp_name'][$i]);
							if($width > $max_width || $height > $max_height)
						{
						print "File height and/or width are too big!";
						exit;
						}
						}

					// ==============
					// Upload Part
					// ==============
					if(is_uploaded_file($_FILES['test']['tmp_name'][$i]))
					{
					move_uploaded_file($_FILES['test']['tmp_name'][$i],$uploaddir.'/'.$_FILES['test']['name'][$i]);
					}
					$i++;
					} //ends while loop 
					// Adds records to database 


					mysql_connect(localhost,$username,$password);
					@mysql_select_db($database) or die ("Unable to connect to database");
					$query = "INSERT INTO stuff SET images='$images'"; 
					mysql_query($query);

					?>

O i almost forgot i am storing the array in a database so i can pull the file name out to use later.. Any help would be appreciated.

    $images=serialize($_FILES['test']);

    does not make sense.

    You are better of pulling the info you need, and storing those in individual fields in the database, so you can later search them, subset them etcetc. Also, when a user wants to modify the records, you do not have to do fancy array manipulations to add images.

    Which directly solves your earlier question: You loop through the $_FILES[] array, and check each file whether they have a name specified. If not, do not process., else move uploaded file & insert details.

    J

      Ahh i see what you are saying. I rewrote the code to make it reflect that.. I'm running into another problem. I need the upload part of the script not to run if the upload fields were left empty. Any idea how to do that? They was that i have it in there now it not working... the revised version on the code.. I added a bit more to it since i was working on image resizing when i stubled upon this problem...

      <?
      require 'dbcon.php';
      
      
      $count=count($_FILES['test']['name']);
      $i=0;
      echo " This is the $count";
      
      // ==============
      // Configuration
      // ==============
      $allowed_ext = "jpg"; // These are the allowed extensions of the files that are uploaded
      $max_size = "50000"; // 50000 is the same as 50kb
      $max_height = "600"; // This is in pixels
      $max_width = "900"; // This is in pixels
      
      $uploaddir = "/home/atlpartylife/atlpartylife.com/test/uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
      	while($i <= $count) {
      						$check = $_FILES['test']['name'][$i];
      						if($check='NULL') //if a file was uploaded in this field
      						{
      							echo "<br>start run $i";
      							// Check Height & Width
      							if ($max_width && $max_height) {
      							list($width, $height, $type, $w) = getimagesize($_FILES['test']['tmp_name'][$i]);
      							if($width > $max_width || $height > $max_height)
      							{
      							print "File height and/or width are too big!";
      							exit;
      							}
      							}
      
      						// ==============
      						// Upload Part
      						// ==============
      						if(is_uploaded_file($_FILES['test']['tmp_name'][$i]))
      						{
      						move_uploaded_file($_FILES['test']['tmp_name'][$i],$uploaddir.'/'.$_FILES['test']['name'][$i]);
      						}
      
      						$images[] = $_FILES['test']['name'][$i];
      						$name=$_FILES['test']['name'][$i];
      						echo " <bR>the name of the file about to be resized $name <br>"; 
      						include("resize.php");
      
      						makeimage($name,'thumbnail_','/home/atlpartylife/atlpartylife.com/test/uploads/',150,100);
      						echo "<br>end run $i";
      						$i++;
      
      						} // ends the if a file was uploaded loop
      
      
      						} //ends while loop 
      						//adds information to the database
      						mysql_connect(localhost,$username,$password);
      						@mysql_select_db($database) or die ("Unable to connect to database");
      						$query = "INSERT INTO stuff SET image0='$images[0]', image1='$images[1]', image2='$images[2]', image3='$images[3]', image4='$images[4]',image5='$images[5]',image6='$images[6]', image7='$images[7]', image8='$images[8]', image9='$images[9]', image10='$images[10]', image11='$images[11]'"; 
      						mysql_query($query)  or die(mysql_error());
      						echo "done";
      
      
      					?>
      

        Nevermind... I ended up using

         $check = $_FILES['test']['size'][$i];
        if($check !='0') //if a file was uploaded in this field
        

          Ok, i am stumped.. I have it so that i can store the information into the database.. Thanks for your suggestion on that.. i have another question for ya. I am trying to run a image resize script that i found. The script run great if i run it from the location of the images that i am resizing, but if i attempt to run it from another location it erros out and says that it can not find the files.. Here is the script that i am trying to use followed by my code to invoke.. Any help would be appreciated..

          <?php
          
          /*
          	Version 1.0 Created by: Ryan Stemkoski
          	Questions or comments: ryan@ipowerplant.com
          	Visit us on the web at: http://www.ipowerplant.com
          	Purpose:  This script can be used to resize one or more images.  It will save the file to a directory and output the path to that directory which you 
          			  can display or write to a databse. 
          
          TO USE, SET: 
          	$filename = image to be resized
          	$newfilename = added to filename to for each use to keep from overwriting images created example thumbnail_$filename is how it will be saved.
          	$path = where the image should be stored and accessed. 
          	$newwidth = resized width could be larger or smaller
          	$newheight = resized height could be larger or smaller
          
          SAMPLE OF FUNCTION: makeimage('image.jpg','fullimage_','imgs/',250,250)
          
          Include the file containing the function in your document and simply call the function with the correct parameters and your image will be resized.
          
          */
          
          //IMAGE RESIZE FUNCTION FOLLOW ABOVE DIRECTIONS
          function makeimage($filename,$newfilename,$path,$newwidth,$newheight) {
          
          //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
          $image_type = strstr($filename, '.');
          
          //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
          	switch($image_type) {
          		case '.jpg':
          			$source = imagecreatefromjpeg($filename);
          			break;
          		case '.png':
          			$source = imagecreatefrompng($filename);
          			break;
          		case '.gif':
          			$source = imagecreatefromgif($filename);
          			break;
          		default:
          			echo("Error Invalid Image Type");
          			die;
          			break;
          		}
          
          //CREATES THE NAME OF THE SAVED FILE
          $file = $newfilename . $filename;
          
          //CREATES THE PATH TO THE SAVED FILE
          $fullpath = $path . $file;
          
          //FINDS SIZE OF THE OLD FILE
          list($width, $height) = getimagesize($filename);
          
          //CREATES IMAGE WITH NEW SIZES
          $thumb = imagecreatetruecolor($newwidth, $newheight);
          
          //RESIZES OLD IMAGE TO NEW SIZES
          imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
          
          //SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100
          imagejpeg($thumb, $fullpath, 100);
          
          //CREATING FILENAME TO WRITE TO DATABSE
          $filepath = $fullpath;
          
          //RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION
          return $filepath;
          
          }
          
          ?> 
          
          
          <?
          require 'dbcon.php';
          include("uploads/resize.php");
          
          
          $count=count($_FILES['test']['name']);
          
          $i=0;
          echo " This is the $count";
          
          // ==============
          // Configuration
          // ==============
          $allowed_ext = "jpg"; // These are the allowed extensions of the files that are uploaded
          $max_size = "50000"; // 50000 is the same as 50kb
          $max_height = "600"; // This is in pixels
          $max_width = "900"; // This is in pixels
          // changed the actual path to "pathtodirectory" since im posting this on a public forum 
          $uploaddir = "/home/pathtodirectory/pathtodirectory/test/uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
          	while($i < $count) {
          						$check = $_FILES['test']['size'][$i];
          						if($check !='0') //if a file was uploaded in this field
          						{
          							echo "<br>start run $i";
          							// Check Height & Width
          							if ($max_width && $max_height) {
          							list($width, $height, $type, $w) = getimagesize($_FILES['test']['tmp_name'][$i]);
          							if($width > $max_width || $height > $max_height)
          							{
          							print "File height and/or width are too big!";
          							exit;
          							}
          							}
          
          						// ==============
          						// Upload Part
          						// ==============
          						if(is_uploaded_file($_FILES['test']['tmp_name'][$i]))
          						{
          						move_uploaded_file($_FILES['test']['tmp_name'][$i],$uploaddir.'/'.$_FILES['test']['name'][$i]);
          						}
          
          						$images[] = $_FILES['test']['name'][$i];
          						$name=$_FILES['test']['name'][$i];
          						echo " <bR>the name of the file about to be resized $name <br>"; 
          
          
          						makeimage($name,'thumbnail_','/test/uploads/',150,100);
          						echo "<br>end run $i";
          
          
          						} // ends the if a file was uploaded loop
          						else{
          
          						}
          						$i++;
          						} //ends while loop 
          						//adds information to the database
          						mysql_connect(localhost,$username,$password);
          						@mysql_select_db($database) or die ("Unable to connect to database");
          						$query = "INSERT INTO stuff SET image0='$images[0]', image1='$images[1]', image2='$images[2]', image3='$images[3]', image4='$images[4]',image5='$images[5]',image6='$images[6]', image7='$images[7]', image8='$images[8]', image9='$images[9]', image10='$images[10]', image11='$images[11]'"; 
          						mysql_query($query)  or die(mysql_error());
          						echo "done";
          
          
          					?>
          

            It's because makeimage is only working with $filename which means it looks for the image in the current directory.

            I'd use $fname = $path.$filename; and then in the switch statement and getimagesize when creating the image use $fname instead of $filename.

            Your correctly storing the thumbnail in the sub-directory but not reading the image from the sub-directory

              Sweet that worked. Thanks for your help

                Write a Reply...