Uploading Multiple files in the PHP

Discussion in 'PHP' started by multigraphics, Apr 19, 2012.

  1. #1
    Hello people

    I want to upload multiple files using php. What I want is when user click on more upload then it should show more file field browser box so that I can select more files.

    Please help me regarding this.
     
    multigraphics, Apr 19, 2012 IP
  2. Uploadables

    Uploadables Member

    Messages:
    100
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Create file - multiple_upload.php
    [COLOR=#666666][FONT=Verdana]<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<td><strong>multiple Files Upload </strong></td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<td>Select file [/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<td>Select file[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<td>Select file[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]<td align="center"><input type="submit" name="Submit" value="Upload" /></td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</table>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</td>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</form>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</tr>[/FONT][/COLOR]
    [COLOR=#666666][FONT=Verdana]</table>[/FONT][/COLOR]
    Code (markup):
    Create file : multiple_upload_ac.php
    [COLOR=#0000FF][FONT=Verdana]<?php[/FONT][/COLOR][COLOR=#0000FF][FONT=Verdana][COLOR=#009900]//set where you want to store files
    //in this example we keep file in folder upload 
    //$HTTP_POST_FILES['ufile']['name']; = upload file name
    //for example upload file name cartoon.gif . $path will be upload/cartoon.gif[/COLOR]
    $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
    $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
    $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana][COLOR=#009900]//copy file to where you want to store file[/COLOR]
    copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
    copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
    copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
    [/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana][COLOR=#009900]//$HTTP_POST_FILES['ufile']['name'] = file name
    //$HTTP_POST_FILES['ufile']['size'] = file size
    //$HTTP_POST_FILES['ufile']['type'] = type of file[/COLOR]
    echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; 
    echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>"; 
    echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; 
    echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
    echo "<P>";[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; 
    echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; 
    echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; 
    echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
    echo "<P>";[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; 
    echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; 
    echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; 
    echo "<img src=\"$path3\" width=\"150\" height=\"150\">";[/FONT][/COLOR]
    [COLOR=#009900][FONT=Verdana]///////////////////////////////////////////////////////[/FONT][/COLOR]
    [COLOR=#009900][FONT=Verdana]// Use this code to display the error or success.[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
    $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
    $filesize3=$HTTP_POST_FILES['ufile']['size'][2];[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]if($filesize1 && $filesize2 && $filesize3 != 0) 
    {
    echo "We have recieved your files";
    }[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]else {
    echo "ERROR.....";
    }[/FONT][/COLOR]
    [COLOR=#009900][FONT=Verdana]//////////////////////////////////////////////[/FONT][/COLOR]
    [COLOR=#009900][FONT=Verdana]// What files that have a problem? (if found)[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]if($filesize1==0) {
    echo "There're something error in your first file";
    echo "<BR />";
    }[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]if($filesize2==0) {
    echo "There're something error in your second file";
    echo "<BR />";
    }[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]if($filesize3==0) {
    echo "There're something error in your third file";
    echo "<BR />";
    }[/FONT][/COLOR]
    [COLOR=#0000FF][FONT=Verdana]?>[/FONT][/COLOR]
    Code (markup):
    P.S Remember to make a folder called "uploads" and CHMOD to 777
     
    Uploadables, Apr 20, 2012 IP
  3. multigraphics

    multigraphics Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Its giving internal server error, what should i do..???
     
    multigraphics, Apr 20, 2012 IP
  4. Uploadables

    Uploadables Member

    Messages:
    100
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #4
    Sorry i stated wrong, you should create a folder called upload instead of uploads

    My bad. Sorry
     
    Uploadables, Apr 20, 2012 IP
  5. Atif123

    Atif123 Active Member

    Messages:
    576
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Nice effort... but could we do same thing in ajax without refreshing page... can u Plz give me details about that
     
    Atif123, Apr 20, 2012 IP
  6. multigraphics

    multigraphics Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    thank yo so much, all worked fine.
     
    multigraphics, Apr 22, 2012 IP
  7. aayush93

    aayush93 Active Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    58
    #7
    aayush93, Apr 22, 2012 IP