Page 1 of 1

multiple files upload

Posted: Sat Feb 25, 2012 3:53 pm
by YoussefSiblini
Hi,
I want the user to have a chance to upload multiple files into a folder, so I created a jquery code so when the user click add link it add another <input name="ufile[]" type="file" id="ufile[]" size="50" /> into my form and this limited into 5 uploaded files only.
But the problem I am having is if they add all the full files my code work normally, but if they didn't click add and upload one file then I get errors like:

Code: Select all

Notice: Undefined offset: 1 in /home/xxx/public_html/xxx/file-upload.php on line 54
Notice: Undefined offset: 2 in /home/xxx/public_html/xxx/file-upload.php on line 55
Notice: Undefined offset: 3 in /home/xxx/public_html/xxx/file-upload.php on line 56
Notice: Undefined offset: 4 in /home/xxx/public_html/xxx/file-upload.php on line 57
Notice: Undefined offset: 1 in /home/xxx/public_html/xxx/file-upload.php on line 61
Notice: Undefined offset: 2 in /home/xxx/public_html/xxx/file-upload.php on line 62
Notice: Undefined offset: 3 in /home/xxx/public_html/xxx/file-upload.php on line 63
Notice: Undefined offset: 4 in /home/xxx/public_html/xxx/file-upload.php on line 64
Notice: Undefined offset: 1 in /home/xxx/public_html/xxx/file-upload.php on line 68
Notice: Undefined offset: 2 in /home/xxx/public_html/xxx/file-upload.php on line 69
Notice: Undefined offset: 3 in /home/xxx/public_html/xxx/file-upload.php on line 70
Notice: Undefined offset: 4 in /home/xxx/public_html/xxx/file-upload.php on line 71
Here is all my codes I am using:
HTML:

Code: Select all

    <form action="Test.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
	   <div id="Files_uploaded_Container">
         <div id="TextBoxDiv1">
              <input name="ufile[]" type="file" id="ufile[]" size="50" /><a href="#" value="addButton" class="addButton">Add</a>&nbsp;<a href="#" value="removeButton" class="removeButton">Remove</a>
         </div>
	   </div>
	   <input type="submit" name="Sell_Add" value="ADD PRODUCT"/>
    </form> 

[b]PHP:[/b]

Code: Select all

<script type="text/javascript">

$(document).ready(function()
{

    var counter = 2;
    // Add button start
    $(".addButton").live("click", function () 
	{

         if(counter>5)
		 {
              alert("Only 5 files allowed");
              return false;
         }   
         var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
         newTextBoxDiv.html('<input name="ufile[]" type="file" id="ufile[]" size="50" /> <a href="#" value="addButton" class="addButton">Add</a>&nbsp;<a href="#" value="removeButton" class="removeButton">Remove</a>');
         newTextBoxDiv.appendTo("#Files_uploaded_Container");
         counter++;
     });
     //add button ends
	 //Remove button starts
     $(".removeButton").live("click", function () 
	 {
          if(counter==2)
		  {
               alert("No more textbox to remove");
               return false;
          }   
          counter--;
          $("#TextBoxDiv" + counter).remove();

     });
	 //Remove button Ends
});
</script>
JQUERY:

Code: Select all

	if (isset($_POST['Sell_Add'])) 
	{
		//files name
        $filename1 = $HTTP_POST_FILES['ufile']['name'][0];  
        $filename2 = $HTTP_POST_FILES['ufile']['name'][1];  //////////////////  Line 54 start from here ///////////////////////////
        $filename3 = $HTTP_POST_FILES['ufile']['name'][2];
        $filename4 = $HTTP_POST_FILES['ufile']['name'][3];
        $filename5 = $HTTP_POST_FILES['ufile']['name'][4];
		
		//Files Type
        $filetype1 = $HTTP_POST_FILES['ufile']['type'][0];
        $filetype2 = $HTTP_POST_FILES['ufile']['type'][1];
        $filetype3 = $HTTP_POST_FILES['ufile']['type'][2];
        $filetype4 = $HTTP_POST_FILES['ufile']['type'][3];
        $filetype5 = $HTTP_POST_FILES['ufile']['type'][4];

        // Files size
        $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
        $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
        $filesize3=$HTTP_POST_FILES['ufile']['size'][2];
        $filesize4=$HTTP_POST_FILES['ufile']['size'][3];
        $filesize5=$HTTP_POST_FILES['ufile']['size'][4];
        }
Is think if the user does not click to add another file then it's been called from php but that input file does not exist so it is giving me this errors, I am still learning php and this code is one of a tutorials I am trying to do.

Please help.


Youssef

Re: multiple files upload

Posted: Sat Feb 25, 2012 4:28 pm
by Celauran

Code: Select all

        if (isset($_POST['Sell_Add']))
        {
                //files name
        $filename1 = $HTTP_POST_FILES['ufile']['name'][0];  
        $filename2 = $HTTP_POST_FILES['ufile']['name'][1];  //////////////////  Line 54 start from here ///////////////////////////
        $filename3 = $HTTP_POST_FILES['ufile']['name'][2];
        $filename4 = $HTTP_POST_FILES['ufile']['name'][3];
        $filename5 = $HTTP_POST_FILES['ufile']['name'][4];
               
                //Files Type
        $filetype1 = $HTTP_POST_FILES['ufile']['type'][0];
        $filetype2 = $HTTP_POST_FILES['ufile']['type'][1];
        $filetype3 = $HTTP_POST_FILES['ufile']['type'][2];
        $filetype4 = $HTTP_POST_FILES['ufile']['type'][3];
        $filetype5 = $HTTP_POST_FILES['ufile']['type'][4];

        // Files size
        $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
        $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
        $filesize3=$HTTP_POST_FILES['ufile']['size'][2];
        $filesize4=$HTTP_POST_FILES['ufile']['size'][3];
        $filesize5=$HTTP_POST_FILES['ufile']['size'][4];
        }
This can easily be replaced by a foreach loop.

Re: multiple files upload

Posted: Sat Feb 25, 2012 4:48 pm
by YoussefSiblini
Thank you for your reply,

But how would you replace it as I am still learning php, I will appreciate may be code :)

Youssef