Inserting multiple upload file info to db

I have an upload script here that will let me upload 5 images and the info posted into a database. only problem is, i dont know how to make it so it will loop and insert the info for all into a database.

If i set it to 1 upload… it works perfect.

i get the feeling its something like… insert into db SET imgurl="$imgurl[$i]"

if so i just dont know exactly how to write that out so it works correctly…

if not. then im more lost than i could imagine?


// Max size PER file in KB, not bytes for simplicity!

// Max size for all files COMBINED in KB, not bytes for simplicity!

//How many file uploads do you want to allow at a time?

//The name of the uploader…
$websitename=“Contact Box Image Uploader”;

// Use random file names? true=yes (recommended), false=use original file name. Random names will help prevent overwritting of existing files!

// Please keep the array structure.

// Path to files folder. If this fails use $fullpath below. With trailing slash

// Full url to where files are stored. With Trailing Slash

// Only use this variable if you wish to use full server paths. Otherwise leave this empty! With trailing slash

//Use this only if you want to password protect your uploads.


    : Don’t edit below this line unless you know some php. Editing some variables or other stuff could cause undeseriable results!!

// MD5 the password… why not?

// If you set a password this is how they get verified!
If($password) {
If($_POST[‘verify_password’]==true) {
If(md5($_POST[‘check_password’])==$password_md5) {
sleep(1); //seems to help some people.
header(“Location: http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’]);



// The password form, if you set a password and the user has not entered it this will show.
If($password) {
If($_COOKIE[‘phUploader’]!=$password_md5) {
$password_form="<form method=“POST” action="".$_SERVER[‘PHP_SELF’]."">n";

Password Required
Enter Password:

// Function to get the extension a file.
function get_ext($key) {
$key=strtolower(substr(strrchr($key, “.”), 1));
// Cause there the same right?
return $key;

foreach($allow_types AS $extension) {

//Gets rid of the last comma for display purpose..

If($i <= $ext_count-2) {
	$types .="*.".$extension.", ";
} Else {
	$types .="*.".$extension;

unset($i,$ext_count); // why not


// Dont allow post if $password_form has been populated
If($_POST[‘submit’]==true AND !$password_form) {

For($i=0; $i <= $file_uploads-1; $i++) {
	If($_FILES['file']['name'][$i]) {
		// For random names
		} Else {
		//Check if the file type uploaded is a valid file type. 
		If(!in_array($ext, $allow_types)) {
			$error.= "Invalid extension for your file: ".$_FILES['file']['name'][$i].", only ".$types." are allowed.<br />Your file(s) were <b>not</b> uploaded.<br />";
			//Check the size of each file
		} Elseif($size > $max_bytes) {
			$error.= "Your file: ".$_FILES['file']['name'][$i]." is to big. Max file size is ".$max_file_size."kb.<br />Your file(s) were <b>not</b> uploaded.<br />";
			// Check if the file already exists on the server..
		} Elseif(file_exists($folder.$file_name[$i])) {
			$error.= "The file: ".$_FILES['file']['name'][$i]." exists on this server, please try again.<br />Your file(s) were <b>not</b> uploaded.<br />";
	} // If Files

} // For

//Tally the size of all the files uploaded, check if it's over the ammount.
If($total_size > $max_combined_bytes) {
	$error.="The max size allowed for all your files combined is ".$max_combined_size."kb<br />";

// If there was an error take notes here!

If($error) {
} Else {
	// No errors so lets do some uploading!
	For($i=0; $i <= $file_uploads-1; $i++) {
		If($_FILES['file']['name'][$i]) {
			If(@move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i])) {

} Else {
$display_message.=“Couldn’t copy “.$file_name[$i].” to server, please make sure “.$folder.” is chmod 777 and the path is correct.n”;

	} //For
} // Else

} // $_POST AND !$password_form


  • Start the form layout
    :- Please know what your doing before editing below. Sorry for the stop and start php… people requested that I use only html for the form…
<? If($password_form) { Echo $password_form; } Elseif($uploaded==true) {?>

files uploaded

<? For($i=0; $i <= $file_uploads-1; $i++) { If($_FILES['file']['name'][$i]) { $file=$i+1; Echo("File #".$file.": ".$full_url.$file_name[$i]."

n"); $host = "xxxxxxxx"; $username = "xxxxxxxxx"; $password = "xxxxxxxxx"; $db = "xxxxxxxxxxx"; mysql_connect($host,$username,$password) or die ("error"); mysql_select_db($db) or die("error"); $imgurl = "$full_url$file_name[$i]"; $cat = $_POST['cat']; $kwords = $_POST['kwords']; $ctbname = $_POST['ctbname']; mysql_query("INSERT INTO pectboxes (id,ctbname,cat,imgurl,kwords) VALUES ('id','$ctbname','$cat','$imgurl','$kwords')") or die("error inserting into db"); } } ?>
Go Back
<?} Else {?> <?If($display_message){?>
<div align="center" class="error_message"><?=$display_message;?></div>
<br />
                    <b><a href="?page=ctboxes">View Contact Tables</a></b><br /><br />
                    <b>Allowed Types:</b> <?=$types?><br />
		<b>Max size per file:</b> <?=$max_file_size?>kb.<br />
		<b>All files will be reviewed before accepted!</b><br />

<?For($i=0;$i <= $file_uploads-1;$i++) {?>

html that works


                    <input type="hidden" name="submit" value="true" />
		<input type="submit" value=" Upload File(s) " /> &nbsp;
		<input type="reset" name="reset" value=" Reset Form " />

<?php} ?>[/php]

[size=99px]**Added [php] tags for better readability. [/size]

lot of code, can’t find the prob as i don’t know where to look at.

what is it doing/not doing? any errors?

well as it stands right now it works perfect…

when changing the amount of uploads to 5… it will upload the images correctly but not place the info in the database. I need some kind of loop to insert them but im not sure how to make it?

im guessing its a foreach $_POST or something along those lines

i hope this explains a bit more by what i mean?

the insert, is inside a lopp, isn’t it?

so why u wanna konw wherther it has to be inside a loop?

tell us what the loop, u posted, is doing!

[code]For($i=0; $i <= $file_uploads-1; $i++) {

If($_FILES['file']['name'][$i]) {
            Echo("<b>File #".$file.":</b> <a href="".$full_url.$file_name[$i]."" target="_blank">".$full_url.$file_name[$i]."</a><br /><br />n");

$host = “xxxxxxxx”;
$username = “xxxxxxxxx”;
$password = “xxxxxxxxx”;
$db = “xxxxxxxxxxx”;
mysql_connect($host,$username,$password) or die (“error”);
mysql_select_db($db) or die(“error”);

$imgurl = “$full_url$file_name[$i]”;
$cat = $_POST[‘cat’];
$kwords = $_POST[‘kwords’];
$ctbname = $_POST[‘ctbname’];

mysql_query(“INSERT INTO pectboxes (id,ctbname,cat,imgurl,kwords) VALUES (‘id’,’$ctbname’,’$cat’,’$imgurl’,’$kwords’)”) or die(“error inserting into db”);

} [/code]

the only loop is for the filename and showing the file name…

if i have $file_uploads=“1”; this works fine and will post to the database for the 1 upload.

if i have $file_uploads=“5”; it will upload the 5 images, show the url to each one but not insert my data into the database.

so do i need to make an additional loop for the data insertion or can i just use the same loop? im a bit lost on t his part.

If I understand what you want to do you will need to create a for loop with a query that inserts the appropriate data into the database.

If you are updating you are going to use something like:

$q=“UPDATE mytable SET myfield1 = ‘$myValue’, $myfield2 = ‘$myValue2’ WHERE myID = ‘$id’”;

Or if you are inserting something:

$q=“INSERT INTO mytable (field1, field2, field3) VALUES (’$myValue1’, ‘$myValue2’, ‘$myValue3’)”;

Hope that helps.

Thanks… that tip got me to thinking… it works great now… heres what i ended up doing…

changed part to this…

$imgurl = "$full_url$file_name[$i]"; $ctname = $_POST['ctbname']; $ctbname = $ctname[$i]; $caty = $_POST['cat']; $cat = $caty[$i]; $kwords = $kword[$i]; $kword = $_POST['kwords'];

and on my form… on the inputs… i had to add [] to it so its like this now… name=“cat[]”

Sponsor our Newsletter | Privacy Policy | Terms of Service