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?
[php]<?php
error_reporting(7);
// Max size PER file in KB, not bytes for simplicity!
$max_file_size=“1024”;
// Max size for all files COMBINED in KB, not bytes for simplicity!
$max_combined_size=“2048”;
//How many file uploads do you want to allow at a time?
$file_uploads=“1”;
//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!
$random_name=true;
// Please keep the array structure.
$allow_types=array(“jpg”,“gif”,“png”);
// Path to files folder. If this fails use $fullpath below. With trailing slash
$folder="./uploads/ctboxes/";
// Full url to where files are stored. With Trailing Slash
$full_url=“http://www.er0x.byethost6.com/test/uploads/ctboxes/”;
// Only use this variable if you wish to use full server paths. Otherwise leave this empty! With trailing slash
$fullpath="";
//Use this only if you want to password protect your uploads.
$password="";
/*
//================================================================================
- ! ATTENTION !
//================================================================================
: 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?
$password_md5=md5($password);
// 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) {
setcookie(“phUploader”,$password_md5,time()+86400);
sleep(1); //seems to help some people.
header(“Location: http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’]);
exit;
}
}
}
// The password form, if you set a password and the user has not entered it this will show.
$password_form="";
If($password) {
If($_COOKIE[‘phUploader’]!=$password_md5) {
$password_form="<form method=“POST” action="".$_SERVER[‘PHP_SELF’]."">n";
$password_form.=“
Password Required | n”;|
Enter Password: | n”;n”; | n”; $password_form.=“n”; $password_form.=“n”; $password_form.=“ | n”;
$password_form.=“n”;
}
}
// Function to get the extension a file.
function get_ext($key) {
$key=strtolower(substr(strrchr($key, “.”), 1));
// Cause there the same right?
$key=str_replace(“jpeg”,“jpg”,$key);
return $key;
}
$ext_count=count($allow_types);
$i=0;
foreach($allow_types AS $extension) {
//Gets rid of the last comma for display purpose..
If($i <= $ext_count-2) {
$types .="*.".$extension.", ";
} Else {
$types .="*.".$extension;
}
$i++;
}
unset($i,$ext_count); // why not
$error="";
$display_message="";
$uploaded==false;
// 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]) {
$ext=get_ext($_FILES['file']['name'][$i]);
$size=$_FILES['file']['size'][$i];
$max_bytes=$max_file_size*1024;
// For random names
If($random_name){
$file_name[$i]=time()+rand(0,100000).".".$ext;
} Else {
$file_name[$i]=$_FILES['file']['name'][$i];
}
//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.
$total_size=array_sum($_FILES['file']['size']);
$max_combined_bytes=$max_combined_size*1024;
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) {
$display_message=$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])) {
$uploaded=true;
} 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…
*/
?>
files uploaded
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) " />
<input type="reset" name="reset" value=" Reset Form " />
<?php} ?>[/php]
[size=99px]**Added [php] tags for better readability. [/size]