Upload Multiple Files

Hi all, I seem to be having a little problem uploading multiple files, they file name isn’t entering the database or uploading but the path is going into the database, but no filename. Not sure what I’m quite doing wrong, any help would be appreciated as always! See below for my code:

<?php
switch($_GET['section']):

	case "galleryAdd":?>
	<div id="galleryName">Add Images to Gallery</div>
	<?php
	if($_POST['submit']):
	foreach($_POST['imgs'] as $id=>$data):
	$uploadDir = "images/galleries/";
	
	foreach($_FILES['imgs'] as $id=>$key):
	$target = $uploadDir.basename($key['main']);
	$targetThumb = $uploadDir.basename($key['thumb']);
	
	move_uploaded_file($key['main']['tmp_name'],$target);
	move_uploaded_file($key['thumb']['tmp_name'],$targetThumb);
	endforeach;
	
	$category = mysql_real_escape_string($data['category']);
		$imgTitle = mysql_real_escape_string($data['name']);
	
	//Once images have been copied to folder...
	$addImage = db_query("INSERT INTO portfolio (category, date_added, image_url, image_tmb, image_title, visible) VALUES ('$category', now(), '/$target', '/$targetThumb', '$imgTitle', '1')");
	endforeach;
	endif;
	foreach (range(1,3) as $num):
	?>
	<form action="index.php?main_page=admin&section=galleryAdd" method="post" enctype="multipart/form-data">
	Image Description<br/>
	<input type="text" class="adminTextField" name="imgs[<?=$num?>][name]"><br/>
	Main Image<br/>
	<input type="file" name="imgs[<?=$num?>][main]"><br/>
	Thumb Image<br/>
	<input type="file" name="imgs[<?=$num?>][thumb]"><br/>
	<select name="imgs[<?=$num?>][category]">
	<option name="interiors" value="interiors">Interiors</option>
		<option name="nightlife" value="nightlife">Nightlife</option>
			<option name="studio" value="studio">Studio</option>
				<option name="bitsandpieces" value="bits and pieces">Bits & Pieces</option>
	</select><br/>
	<input type="submit" value="Upload" name="submit">
	</form>
	<?php 
	endforeach;
	break;

endswitch;
?>

$name_file = $_FILES[‘imgs’][‘name’];// store file name in a variable

move_uploaded_file($key[‘main’][‘tmp_name’],$target.$name_file); // add it to upload dir

Hey dude, I’ve just managed to get this to work but I’m trying to add the file names and path to the database which is sort of working - I’m using two foreach’s to go through the two images types but they’re not entering the database. I know what I’m doing wrong but I can’t really explain or put it into words. Anybody got a solution to this problem?

if($_POST['submit']):
	//Upload main images...
	foreach($_FILES['main']['name'] as $id=>$keyy):
	$uploadDir = "images/galleries/";
		$target = $uploadDir.basename($keyy);
			$tmp = $_FILES['main']['tmp_name'][$id];
				move_uploaded_file($tmp,$target);
	endforeach;
	//Upload Thumbnails...
	foreach($_FILES['thumb']['name'] as $id=>$keyy):
	$uploadDir = "images/galleries/";
		$targetThumb = $uploadDir.basename($keyy);
			$tmp = $_FILES['thumb']['tmp_name'][$id];
				move_uploaded_file($tmp,$targetThumb);
	endforeach;
		
	$category = mysql_real_escape_string($_POST['category']);
		$imgTitle = mysql_real_escape_string($_POST['name']);
	
	//Once images have been copied to folder...
	$addImage = db_query("INSERT INTO portfolio (category, date_added, image_url, image_tmb, image_title, visible) VALUES ('$category', now(), '/$target', '/$targetThumb', '$imgTitle', '1')");
	
	endif;

yep, give me 2 minutes to write it up though!

try:


<div id="galleryName">Add Images to Gallery</div>
    <?php
    if($_POST['submit']):
    
    // we are looping so we need a counter....
    $i = 1;
    $uploadDir = "images/galleries/";   
    
    foreach($_POST['imgs'] as $id=>$data):

        $category = mysql_real_escape_string($data['category']);
        $imgTitle = mysql_real_escape_string($data['name']);
    
        $target = $uploadDir.basename($_FILES['imgs']['name'][$i]['main']);
        $targetThumb = $uploadDir.basename($_FILES['imgs']['name'][$i]['thumb']);
       
        move_uploaded_file($_FILES['imgs']['name'][$i]['tmp_name'],$target);
        move_uploaded_file($_FILES['imgs']['name'][$i]['tmp_name'],$targetThumb);
    
        //Once images have been copied to folder...
        $addImage = "
            INSERT 
                INTO 
            portfolio 
                (category
                , date_added
                , image_url
                , image_tmb
                , image_title
                , visible
                ) VALUES (
                '$category'
                , now()
                , '/$target'
                , '/$targetThumb'
                , '$imgTitle'
                , '1'
            )";
        // increment the counter for each loop
        $i++;
        
        $q = db_query($addImage);
    endforeach;
    endif;
    
    
    /** 
     * form action etc need to be outside the loop otherwise you get 3 different forms!
     * */
    ?>
    
    <form action="index.php?main_page=admin&section=galleryAdd" method="post" enctype="multipart/form-data">
    
    <?php
    foreach (range(1,3) as $num):
    ?>
    
    Image Description<br/>
    <input type="text" class="adminTextField" name="imgs[<?php echo $num; ?>][name]"><br/>
    Main Image<br/>
    <input type="file" name="imgs[<?php echo $num; ?>][main]"><br/>
    Thumb Image<br/>
    <input type="file" name="imgs[<?php echo $num; ?>][thumb]"><br/>
    <select name="imgs[<?php echo $num; ?>][category]">
        <option value="interiors">Interiors</option>
        <option value="nightlife">Nightlife</option>
        <option value="studio">Studio</option>
        <option value="bits and pieces">Bits & Pieces</option>
    </select><br/>
    <?php 
    endforeach;
    ?>
    
    <input type="submit" value="Upload" name="submit">
    </form>

Hey bud, wow that’s just great, it’s working perfectly, just the sort of thing I was looking for. Must of had a kind mental block! That really is appreciated, you enjoy the rest of your morning :smiley: :lol:

You are welcome :slight_smile:

Hey, apologies to drag this back up, I’ve just noticed that it’s not moving the files to the folder, I’ve echoed $target and I can see the file has been reconized but just has not moved. I do have full read/write access as I was uploading images beforehand…any ideas?

Hey there, I’ve managed to work out what the problem was, the tmp_name was in the wrong location in the move_uploaded_file function. So I’ve changed it to the following and working perfectly. Thanks again :smiley:

move_uploaded_file($_FILES['imgs']['tmp_name'][$i]['main'],$target);
        move_uploaded_file($_FILES['imgs']['tmp_name'][$i]['thumb'],$targetThumb);