Jump to content

multiplie file upload with php


yobo

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.