liamloveslearning Posted May 23, 2010 Share Posted May 23, 2010 Hi everyone, Ive builr a multiple image uploader. the uploader works but it doesnt isert the file names accordingly, say theres 3 upload fields, it only inserts the first filename in all 3 database records, can anybody nit pick my code and see obvious reasons? Thanks <?php /*Linecraft PHP File Upload*/ if (isset($_POST['MM_insert'])) { $upload_error_codes=array("", "The uploaded file exceeds the upload_max_filesize directive in php.ini.","", "The uploaded file was only partially uploaded.", "No file was uploaded.","Missing a temporary folder.", "Failed to write file to disk.","File upload stopped by extension."); $allowed_ext_string="jpg,jpeg,png,tiff,bmp"; $allowed_extensions=explode(",",$allowed_ext_string); $upload_status = ""; $allowed_size = 6+0; $success_page = "model-account.php?message=barry"; $thumbs_dir = ""; $resize_image = ""; $resize_width = +0; $resize_height = +0; $thumb_width = +0; $thumb_height = +0; $make_thumbs = ""; $thumb_prefix = ""; $thumb_suffix = ""; $file_prefix = ""; $file_suffix = ""; $append_date_stamp = ""; $date_stamp=($append_date_stamp=="1")?date(time()):""; $haulted = false; $upload_folder="model_pictures/"; //Check for restrictions //Check if upload folder exists if(!file_exists($upload_folder)){die("Upload folder doesn't exist");} if(!is_writable($upload_folder)){die("Upload folder is not writable");} if($make_thumbs == "yes" && !file_exists($thumbs_dir)){die("Thumbnails folder doesn't exist");} if($make_thumbs == "yes" && !is_writable($thumbs_dir)){die("Thumbnails folder is not writable");} foreach($_FILES as $files => $_file){ //Check if it's not empty if($_file['name']!=""){ $pathinfo = pathinfo($_file['name']); //If allowed extension or no extension restriction if(!in_array(strtolower($pathinfo['extension']),$allowed_extensions) && $allowed_ext_string!=""){ die(strtoupper($pathinfo['extension'])." files are not allowed. <br>No files have been uploaded."); } if($_file['size']>$allowed_size*1048576 && $allowed_size!=0){ die("The file size of ".basename($_file['name'])." is ".round($_file['size']/1048576,2)."MB, which is larger than allowed ".$allowed_size."MB.<br>No files have been uploaded."); } } } //All checks passed, attempt to upload foreach($_FILES as $files => $_file){ //Check if it's not empty if($_file['name']!=""){ $pathinfo = pathinfo($_file['name']); $file_name_array = explode(".", basename($_file['name'])); $filename = $file_name_array[count($file_name_array)-2]; $target = $upload_folder; $file_uploaded = false; $target = $target."/".$file_prefix.$filename.$file_suffix.$date_stamp.".".$pathinfo['extension']; //if image if(strtolower($pathinfo['extension'])=="jpeg" || strtolower($pathinfo['extension'])=="jpg"){ //if needs resizing or a thumbnail if(($resize_image == "yes" && ($resize_width!="" || $resize_height!="")) || ($make_thumbs == "yes" && ($thumb_width!="" || $thumb_height!=""))){ $src = imagecreatefromjpeg($_file['tmp_name']); list($width,$height)=getimagesize($_file['tmp_name']); //if needs thumbnail if ($make_thumbs == "yes" && ($thumb_width!="" || $thumb_height!="")){ $thumb_newwidth=($thumb_width!=0)?$thumb_width:(($width/$height)*$thumb_height); $thumb_newheight=($thumb_height!=0)?$thumb_height:(($height/$width)*$thumb_width); $tmp=imagecreatetruecolor($thumb_newwidth,$thumb_newheight); imagecopyresampled($tmp,$src,0,0,0,0,$thumb_newwidth,$thumb_newheight,$width,$height); $thumb_name=$thumb_prefix.$filename.$thumb_suffix.$date_stamp.".".$pathinfo['extension']; if(imagejpeg($tmp,$thumbs_dir."/".$thumb_name,100)){ $upload_status=$upload_status."Thumbnail for ".basename($_file['name'])." was created successfully.<br>"; }else{ die($upload_status."There was a problem creating a thumbnail for ". basename($_file['name']).". Upload was interrupted.<br>"); } } //if needs resizing if($resize_image == "yes" && ($resize_width!="" || $resize_height!="")){ $newwidth=($resize_width!=0)?$resize_width:(($width/$height)*$resize_height); $newheight=($resize_height!=0)?$resize_height:(($height/$width)*$resize_width); $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); if(imagejpeg($tmp,$target,100)){ $upload_status=$upload_status.basename($_file['name'])." was successfully resized.<br>"; $file_uploaded=true; }else{ die($upload_status.basename($_file['name'])." could not be resized. Upload was interrupted.<br>"); } } } } if(!$file_uploaded){ if(move_uploaded_file($_file['tmp_name'], $target)){ $upload_status=$upload_status.basename($_file['name'])." was uploaded successfully.<br>"; }else{ $haulted=true; } } //Cleanup if(isset($src)){imagedestroy($src);unset($src);} if(isset($tmp)){imagedestroy($tmp);unset($tmp);} if($haulted){die($upload_status."There was a problem uploading ". basename($_file['name']).". Error: ".$upload_error_codes[basename($_file['error'])].". Upload was interrupted.<br>");} } } if($success_page!="" && $upload_status!=""){ header("Location: ".$success_page); } } /*Linecraft PHP File Upload*/ if (isset($_POST['MM_Insert'])) { $upload_error_codes=array("", "The uploaded file exceeds the upload_max_filesize directive in php.ini.","", "The uploaded file was only partially uploaded.", "No file was uploaded.","Missing a temporary folder.", "Failed to write file to disk.","File upload stopped by extension."); $allowed_ext_string="jpg,jpeg,png,tiff,bmp"; $allowed_extensions=explode(",",$allowed_ext_string); $upload_status = ""; $allowed_size = 6+0; $success_page = "model-account.php?message=barry"; $thumbs_dir = ""; $resize_image = ""; $resize_width = +0; $resize_height = +0; $thumb_width = +0; $thumb_height = +0; $make_thumbs = ""; $thumb_prefix = ""; $thumb_suffix = ""; $file_prefix = ""; $file_suffix = ""; $append_date_stamp = ""; $date_stamp=($append_date_stamp=="1")?date(time()):""; $haulted = false; $upload_folder="model_pictures/"; //Check for restrictions //Check if upload folder exists if(!file_exists($upload_folder)){die("Upload folder doesn't exist");} if(!is_writable($upload_folder)){die("Upload folder is not writable");} if($make_thumbs == "yes" && !file_exists($thumbs_dir)){die("Thumbnails folder doesn't exist");} if($make_thumbs == "yes" && !is_writable($thumbs_dir)){die("Thumbnails folder is not writable");} foreach($_FILES as $files => $_file){ //Check if it's not empty if($_file['name']!=""){ $pathinfo = pathinfo($_file['name']); //If allowed extension or no extension restriction if(!in_array(strtolower($pathinfo['extension']),$allowed_extensions) && $allowed_ext_string!=""){ die(strtoupper($pathinfo['extension'])." files are not allowed. <br>No files have been uploaded."); } if($_file['size']>$allowed_size*1048576 && $allowed_size!=0){ die("The file size of ".basename($_file['name'])." is ".round($_file['size']/1048576,2)."MB, which is larger than allowed ".$allowed_size."MB.<br>No files have been uploaded."); } } } //All checks passed, attempt to upload foreach($_FILES as $files => $_file){ //Check if it's not empty if($_file['name']!=""){ $pathinfo = pathinfo($_file['name']); $file_name_array = explode(".", basename($_file['name'])); $filename = $file_name_array[count($file_name_array)-2]; $target = $upload_folder; $file_uploaded = false; $target = $target."/".$file_prefix.$filename.$file_suffix.$date_stamp.".".$pathinfo['extension']; //if image if(strtolower($pathinfo['extension'])=="jpeg" || strtolower($pathinfo['extension'])=="jpg"){ //if needs resizing or a thumbnail if(($resize_image == "yes" && ($resize_width!="" || $resize_height!="")) || ($make_thumbs == "yes" && ($thumb_width!="" || $thumb_height!=""))){ $src = imagecreatefromjpeg($_file['tmp_name']); list($width,$height)=getimagesize($_file['tmp_name']); //if needs thumbnail if ($make_thumbs == "yes" && ($thumb_width!="" || $thumb_height!="")){ $thumb_newwidth=($thumb_width!=0)?$thumb_width:(($width/$height)*$thumb_height); $thumb_newheight=($thumb_height!=0)?$thumb_height:(($height/$width)*$thumb_width); $tmp=imagecreatetruecolor($thumb_newwidth,$thumb_newheight); imagecopyresampled($tmp,$src,0,0,0,0,$thumb_newwidth,$thumb_newheight,$width,$height); $thumb_name=$thumb_prefix.$filename.$thumb_suffix.$date_stamp.".".$pathinfo['extension']; if(imagejpeg($tmp,$thumbs_dir."/".$thumb_name,100)){ $upload_status=$upload_status."Thumbnail for ".basename($_file['name'])." was created successfully.<br>"; }else{ die($upload_status."There was a problem creating a thumbnail for ". basename($_file['name']).". Upload was interrupted.<br>"); } } //if needs resizing if($resize_image == "yes" && ($resize_width!="" || $resize_height!="")){ $newwidth=($resize_width!=0)?$resize_width:(($width/$height)*$resize_height); $newheight=($resize_height!=0)?$resize_height:(($height/$width)*$resize_width); $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); if(imagejpeg($tmp,$target,100)){ $upload_status=$upload_status.basename($_file['name'])." was successfully resized.<br>"; $file_uploaded=true; }else{ die($upload_status.basename($_file['name'])." could not be resized. Upload was interrupted.<br>"); } } } } if(!$file_uploaded){ if(move_uploaded_file($_file['tmp_name'], $target)){ $upload_status=$upload_status.basename($_file['name'])." was uploaded successfully.<br>"; }else{ $haulted=true; } } //Cleanup if(isset($src)){imagedestroy($src);unset($src);} if(isset($tmp)){imagedestroy($tmp);unset($tmp);} if($haulted){die($upload_status."There was a problem uploading ". basename($_file['name']).". Error: ".$upload_error_codes[basename($_file['error'])].". Upload was interrupted.<br>");} } } if($success_page!="" && $upload_status!=""){ header("Location: ".$success_page); } } ?> <?php require_once('Connections/saucy_connection.php'); ?> <?php // Load the tNG classes require_once('includes/tng/tNG.inc.php'); // Make unified connection variable $conn_saucy_connection = new KT_connection($saucy_connection, $database_saucy_connection); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } foreach($_FILES as $files => $_file){ $_POST[$files]=""; if($_file['name']!=""){ $pathinfo=pathinfo($_file['name']); $file_name_array = explode(".", basename($_file['name'])); $filename = $file_name_array[count($file_name_array)-2]; $_POST[$files]=$file_prefix.$filename.$file_suffix.$date_stamp.".".$pathinfo['extension']; if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadim")) { $insertSQL = sprintf("INSERT INTO model_pictures (user_id, user_picture) VALUES (%s, %s)", GetSQLValueString($_POST['userid'], "text"), GetSQLValueString($_POST['upl1'], "text")); mysql_select_db($database_saucy_connection, $saucy_connection); $Result1 = mysql_query($insertSQL, $saucy_connection) or die(mysql_error()); } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/saucytemplate.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- InstanceBeginEditable name="doctitle" --> <title>Saucy Photos 2010</title> <!-- InstanceEndEditable --> <link href="styles/nav_style.css" rel="stylesheet" type="text/css" /> <link href="styles/global_style.css" rel="stylesheet" type="text/css" /> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <div id="container"> <div id="wrapper"> <div id="header"></div> <div id="main_nav"> <ul> <li><a href="index.php" title="Saucy Girls">Home</a></li> <li><a href="model-showcase.php" title="Models Showcase">Showcase</a></li> <li><a href="coming-soon.php" title="Coming Soon">Coming Soon</a></li> <li><a href="competitions.php" title="Competitions">Competitions</a></li> <!--- <li><a href="#" title="Saucy Store">Store</a></li> ---> <?php //Show If User Is Logged In (region1) $isLoggedIn = new tNG_UserLoggedIn($conn_saucy_connection); //Grand Levels: Level $isLoggedIn->addLevel("1"); if ($isLoggedIn->Execute()) { ?> <li><a href="model-account.php" title="Models Account">My Account</a></li> <li><a href="logout.php" title="Logout">Logout</a></li> <?php // else Show If User Is Logged In (region1) } else { ?> <li><a href="model-login.php" title="Saucy Models Login">Models Login</a></li> <?php } //End Show If User Is Logged In (region1) ?> </ul> </div> <div id="main"><!-- InstanceBeginEditable name="Content" --> <div id="toprow"> <form action="<?php echo $editFormAction; ?>" method="POST" name="uploadim" enctype="multipart/form-data"> <input type="hidden" name="userid" value="<?php echo $_SESSION['kt_login_id']; ?>" /> <input type="file" name="upl1" /> <input type="file" name="upl2" /> <input type="file" name="upl3" /> <input type="submit" value="MM_Insert" /> <input type="hidden" name="MM_insert" value="uploadim" /> </form> </div> <!-- InstanceEndEditable --></div><div class="clear"></div> <div id="footer"><div style="float:left">Copyright ©2010 Saucyphotos.com<br>Web design and development by <a target="_blank" href="http://www.signworldgroup.com/web-design.html">Signworld Chester Web Design</a></div><div style="float:right;"><a href="http://www.saucyphotos.com/terms.php">Terms</a> / <a href="http://twitter.com/saucyphotoscom">Twitter</a> / <a href="http://www.facebook.com/pages/Saucyphotoscom/121730814523368">Facebook</a></div><div style="clear:both;"></div></div> </div> </div> </body> <!-- InstanceEnd --></html> Link to comment https://forums.phpfreaks.com/topic/202616-looper-not-inserting-record-names-properly/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.