yobo Posted July 19, 2011 Share Posted July 19, 2011 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. <!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> the 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 Quote Link to comment https://forums.phpfreaks.com/topic/242375-multiplie-file-upload-with-php/ Share on other sites More sharing options...
IrOnMaSk Posted July 19, 2011 Share Posted July 19, 2011 this should solve your problem Uploading Multiple Files http://www.php.net/manual/en/features.file-upload.multiple.php Quote Link to comment https://forums.phpfreaks.com/topic/242375-multiplie-file-upload-with-php/#findComment-1244927 Share on other sites More sharing options...
yobo Posted July 20, 2011 Author Share Posted July 20, 2011 Hey dude, that dont really help me? since there examples have hard coded mutiple input fileds for files where as mine does not mine uses jquery and php to allow for one file to be uploaded mandatory and an option to add another upload file input box if necssary. Joe Quote Link to comment https://forums.phpfreaks.com/topic/242375-multiplie-file-upload-with-php/#findComment-1245032 Share on other sites More sharing options...
IrOnMaSk Posted July 20, 2011 Share Posted July 20, 2011 Joe, I don't think you will ever find the exact example code for what you looking for, unless you pay someone to do it and you post the code up then the next guy having same problem will have your code as the example to use So you should put the pieces together. You stated that php doesn't take the upload file and save it to the database. That seems like bug in the file upload code (check the manual). Or the jquery, javascript, and php don't even talk to each other at all. I'm replying so your post will end up back in the first page, so people can see and they might want to help. by now this post probly is at the 5 or 6th page already!!! goodluck Quote Link to comment https://forums.phpfreaks.com/topic/242375-multiplie-file-upload-with-php/#findComment-1245160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.