Hey all,
I have created an upload script that allows the user to upload a file using this same page if the user wants to upload another file at the same time they click a button and it will create an extra file upload box built using jquery. now this seems to work but I am having problems with my php code taking the upload file(s) add adding them to the database. I have attached my Code along with the javascript code that i am using.
My php code
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="ohyeah" />
<title>Untitled 1</title>
<script src="includes/jquery.js" type="text/javascript"></script>
<script src="includes/upload.js" type="text/javascript"></script>
</head>
<body>
<?php
/**
* @author ohyeah
* @copyright 2011
*/
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
foreach($_FILES['userfile']['error'] as $key => $error)
{
$tmpName = $_FILES['userfile']['tmp_name'][$key];
$fileName = $_FILES['userfile']['name'][$key];
$fileSize = $_FILES['userfile']['size'][$key];
$fileType = $_FILES['userfile']['type'][$key];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
}
//include 'library/config.php';
include ('includes/dbconnect.php');
$query = "INSERT INTO uploads (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysqli_query($dbc, $query);
mysqli_close($dbc);
echo "<br>File $fileName uploaded<br>";
}
?>
<form action="upload2.php" method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<div id="text">
<div ><input name="userfile[]" type="file" /></div>
<!-- This is where the new file field will appear -->
</div>
<input type="button" id="add-file-field" name="add" value="Add input field" />
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload " /></td>
</tr>
</table>
</form>
</body>
</html>
my java script code
$(document).ready(function(){
// This will add new input field
$("#add-file-field").click(function(){
$("#text").append("<div class='added-field'><input name='userfile[]' type='file' /><input type='button' class='remove-btn' value='Remove Field' /></div>");
});
// The live function binds elements which are added to the DOM at later time
// So the newly added field can be removed too
$(".remove-btn").live('click',function() {
$(this).parent().remove();
});
});
thanks for your help- Joe