Page 1 of 1

how uploading multiple files in php and mysql

Posted: Sun Apr 19, 2009 8:03 pm
by ranheaven
hi,

i've been struggling for several days now on uploading multiple files using php and save it into mysql database i know this kinda old but those answers that i get from forums didn't worked for me...

here's my script in uploading multiple files using html form and javascript i got it on http://www.petefreitag.com/item/587.cfm it uploads like gmail attachments

this is my form:
<tr><td align="right">Attachment(s): </td>
<td>
<input type="hidden" name="MAX_FILE_SIZE3" value="300000">
<input name="userfile3[]" type="file" id="userfile3" size="50" onchange="document.getElementById('moreUploadsLink').style.display = 'block'; document.ticketform.getfile3.value='NOT NULL'; document.ticketform.ctr.value='0';" /><a href="javascript:clearUpload();" temp_href="javascript:clearUpload();">cancel</a>
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;"><a href="javascript:addFileInput();" temp_href="javascript:addFileInput();">Attach another File</a></div>
<input type="text" id="gfile3" name="getfile3" />
<input type="text" name="ctr" /> (--> i've made a counter so that i can trace how many times i would loop)
</td>
</tr>

and this is the javascript that used to append a child:
var upload_number = 0;
function addFileInput(){
var d = document.createElement("div");
var l = document.createElement("a");
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "userfile3"[upload_number]);
file.setAttribute("size", "50");
l.setAttribute("href", "javascript:removeFileInput('f"+upload_number+"');");
l.appendChild(document.createTextNode("remove")); d.setAttribute("id", "f"+upload_number);
d.appendChild(file); d.appendChild(l); document.getElementById("moreUploads").appendChild(d);
upload_number++;
document.ticketform.ctr.value=upload_number;
}
function removeFileInput(i){
var elm = document.getElementById(i);
document.getElementById("moreUploads").removeChild(elm);
upload_number--;
document.ticketform.ctr.value=upload_number;
}

and here's my code on how to save it from mysql:
//insert attachment
$count=0;
while($count != $ctr){
require("connect_db.php");
$fileName = $_FILES['userfile3']['name'][$count];
$tmpName = $_FILES['userfile3']['tmp_name'][$count];
$fileSize = $_FILES['userfile3']['size'][$count];
$fileType = $_FILES['userfile3']['type'][$count];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$fetch2 = ("INSERT INTO attachment2 (
tech_id,
FileName,
FileMime,
Filesize,
FileData,
Created)
VALUES (
'$tech_id',
'$fileName',
'$fileType',
'$fileSize',
'$content',
NOW())
")
or die(mysql_error());
mysql_query($fetch2) or die(mysql_error());
mysql_close();
$count++;
}

Thanks hope someone will help me :)

-R