Jump to content

Add an additional field to the db upload form


VoodooJai

Recommended Posts

Hi all again

I have an upload form and script that adds multiple files to a db and all works ok, but the description part of  the form just duplicates the first entry multiple times.

I have been studying the code but are stuck.

which part of the code do I need to edit to have it work as I want it to.

 

<?php

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

foreach($_FILES as $files => $_file){
$_POST['File_Name']=basename($_file['name']);
if($_POST['File_Name']!=""){

// ********************************************************************************

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO uploads (file_name, user_ID, fileNR, `description`) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($dbPrefix.$_POST['File_Name'], "text"),
                       GetSQLValueString($_POST['UID'], "int"),
                       GetSQLValueString($_POST['FileNR'], "text"),
                       GetSQLValueString($_POST['Desc'], "text"));

  mysql_select_db($database_tattoocv, $tattoocv);
  $Result1 = mysql_query($insertSQL, $tattoocv) or die(mysql_error());
}

}}

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";
$allowed_extensions=explode(",",$allowed_ext_string);
$upload_status = "";
$allowed_size =  2+0;
$success_page = "";
$thumbs_dir = "uploads/photo/thumbnail/";
$resize_image = "yes";
$resize_width = 400+0;
$resize_height = +0;
$thumb_width = 150+0;
$thumb_height = +0;		
$make_thumbs = "yes";
// ********************************************************************
// Var to prefix the filname with a userID and microtime
// ********************************************************************
//	$FilePrefix = "ID_".$UserNameID."_";
//    $MyPrefix = generatePassword($length,$strength);

// Create the prefixes for the 2 uploading files
$FilePrefix = "ID_".$UserNameID."_".$My."_";

$thumb_prefix = "thumb_".$FilePrefix;
$haulted = false;
$upload_folder="uploads/photos/";

//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']!=""){
// ****************************************************************
// adds a prefix to the "files name" but not the db entry			
		$pathinfo = pathinfo($FilePrefix.$_file['name']);

		$target = $upload_folder;
		$file_uploaded = false;
// ****************************************************************
// adds a prefix to the "files name" but not the db entry			
		$target = $target."/".$FilePrefix.basename($_file['name']);

		//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); 
					if(imagejpeg($tmp,$thumbs_dir."/".$thumb_prefix.basename($_file['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)){

//         This section uploads the file(s)
				$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);
}
}

?>

***************          THIS IS THE FORM USED FOR UPLOADS  **************************

<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
        Browse for file(s)
          <input name="File_Name" type="file" size="50" />
        <input name="FileNR" type="hidden" value="<?php echo $pwd; ?>" />
       Add a description
        <textarea name="Desc" cols="25" rows="2">Add a description</textarea>

        <input name="File_Name2" type="file" size="50" />
        <input name="FileNR2" type="hidden" value="<?php echo $pwd; ?>" />
       <textarea name="Desc_2" cols="25" rows="2" id="Desc_2"></textarea></td>

        <input name="File_Name3" type="file" size="50" />
        <input name="FileNR3" type="hidden" value="<?php echo $pwd; ?>" />
       <textarea name="Desc_3" cols="25" rows="2" id="Desc_3"></textarea></td>

        <input name="File_Name4" type="file" size="50" />
        <input name="FileNR4" type="hidden" value="<?php echo $pwd; ?>" />
       <textarea name="Desc_4" cols="25" rows="2" id="Desc_4"></textarea></td>

     <input name="Submit" type="submit" value="Upload file(s) and description(s)" /></td>
     <!-- Passes the random prefix for the upload file names -->

<input name="MyPrefix" type="hidden" value="<?php echo $pwd ?>" />

<input type="hidden" name="UID" value="<?php echo $UserNameID; ?>">
<input type="hidden" name="MM_insert" value="form1">
</form>

 

Hope this helps

 

VoodooJai

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.