Jump to content

[SOLVED] filename being deleted from input field


lisa_85

Recommended Posts

Hi, posting loads of questions today :D  I have linked a script I found online to my database.  The script is for uploading multiple files which it does fine.  The problem is that I want a new row in my DB table tblfiles for each file uploaded.  When I upload, it only inserts a new row for the first file.  How do I get it to run through a loop to make an entry for each file.  This is my code.

function upload(){
foreach($_POST as $key=>$value) {
	$$key = $value;
}
mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')");
$lastinsert = mysql_insert_id();

if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
	$GLOBALS['msg'] = ""; //initiate the global message
	for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
		$filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name
		$path = 'uploads/'.$filen; //generate the destination path
		mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')");
		if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
			$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
		}
	}
}
else {
	$GLOBALS['msg'] = "No files found to upload"; //Failed message	
}
uploadForm(); //display the main form
}

Thanks :)

Hi, I guess the easiest thing is to post the code for the whole page :D

<?php
// DB Connect
$dbHost = "";
$dbUser = "";
$dbPass = "";
$dbname = "";
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);

if($_POST['pgaction']=="upload")
upload();
else
uploadForm();

//The form having dynamic file uploader
function uploadForm() {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title>File Upload</title>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="common/style.css" type="text/css" />
</head>

<body>
<div id="pagewidth">
<div id="header"></div>

<div class="clearfix"> 

<div id="maincol">

<div id="stylized" class="myform">
<form name="frm" method="post" onsubmit="return validate(this);" enctype="multipart/form-data">
<input type="hidden" name="pgaction" />
<input type="hidden" name="uID" value="1" />
<?php if ($GLOBALS['msg']) { echo '<div class="err">'.$GLOBALS['msg'].'</div>'; }?>
<h1>File Upload Form</h1>
<p>Upload files</p>

<label>Title
<span class="small">Add page title</span>
</label>
<input type="text" name="ftitle" />

<label>Description
<span class="small">Add a short description</span>
</label>
<textarea name="fdesc"></textarea>

<label>Upload File
<span class="small">Choose a file</span>
</label>
<div id="listStart">
<input type="file" name="item_file[]" /><a href="javascript:_add_more();" title="Add more"><img src="plus_icon.gif" border="0" /></a>

<div id="listEnd" style="padding-left: 140px;"><input type="submit" value="Upload File(s)" /></div>
</div>
</form>
</div>

<script language="javascript">
<!--
function _add_more(){
	var newRow = document.createElement('<div>');
	var txt = "<div style=\"padding-left:140px\"><input type=\"file\"name=\"item_file[]\"></div>";
	newRow.innerHTML= txt;
	var theDad = document.getElementById("listStart");
	var theEnd= document.getElementById("listEnd");
	theDad.insertBefore(newRow, theEnd);
}
function validate(f){
	var chkFlg = false;
	for(var i=0; i < f.length; i++) {
		if(f.elements[i].type=="file" && f.elements[i].value != "") {
			chkFlg = true;
		}
	}
	if(!chkFlg) {
		alert('Please browse/choose at least one file');
		return false;
	}
	f.pgaction.value='upload';
	return true;
}
//-->
</script>
</div>

<div id="leftcol">Home<p />Logout<p /></div>
</div>

<div id="footer">Copyright © <?php echo date('Y'); ?></div>
</div>
</body>
</html>

<?php
}

//function to store uploaded file

function upload(){
foreach($_POST as $key=>$value) {
	$$key = $value;
}
mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')");
$lastinsert = mysql_insert_id();

if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
	$GLOBALS['msg'] = ""; //initiate the global message
	for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
		$filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name
		$path = 'uploads/'.$filen; //generate the destination path
		mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')");
		if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
			$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
		}
	}
}
else {
	$GLOBALS['msg'] = "No files found to upload"; //Failed message	
}
uploadForm(); //display the main form
}
?>

It uploads all the files as it should but just doesn't make all the entries in the DB table tblfiles.  Thanks :)

Hi, does anyone have any idea how to solve this problem?  I have been trying but am stuck :( Thanks

 

Please change:

$filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name

 

To:

$filen = time().'_'.$_FILES["item_file"]['name'][$j]; //file name

 

And all existence of "$j" in the script.

 

Hope this will help.

 

try this and post the result

 

foreach($_POST as $key=>$value) {
	$$key = $value;
}
mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')");
$lastinsert = mysql_insert_id();

echo count($_FILES["item_file"]['name']);

/*if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
	$GLOBALS['msg'] = ""; //initiate the global message
	for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
		$filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name
		$path = 'uploads/'.$filen; //generate the destination path
		mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')");
		if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
			$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
		}
	}
}
else {
	$GLOBALS['msg'] = "No files found to upload"; //Failed message	
}
uploadForm(); //display the main form*/

ok cool, just ruling things out.

 

uncomment the stuff i commented out

 

/*if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
	$GLOBALS['msg'] = ""; //initiate the global message
	for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
		$filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name
		$path = 'uploads/'.$filen; //generate the destination path
		mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')");
		if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
			$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
		}
	}
}
else {
	$GLOBALS['msg'] = "No files found to upload"; //Failed message	
}
uploadForm(); //display the main form*/

 

and change this line

mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')");

 

to this and see if you get any errors

mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')") or die("error: ".mysql_error());			

 

Hiya, sorry I have not responded to this for a while I have been on holiday :)  I did what you said and I get this: "error: Duplicate entry '29' for key 1."  Also I noticed that it only uploads 1 file when the error checking is on.  Do you know what is wrong?  Thanks :)

Ooooh, I am so thick lol :)  Just realised what the problem was.  It is because the field in my table should be able to have duplicate values but I must have set it as a primary key by mistake.  I added a dummy primary key field and now it is working woohoo :)  Thanks for all your help guys :)

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.