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§ion=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;
?>
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;
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
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