Multiple file upload in php

前端 未结 14 1619
轮回少年
轮回少年 2020-11-21 11:32

I want to upload multiple files and store them in a folder and get the path and store it in the database... Any good example you looked for doing multiple file upload...

相关标签:
14条回答
  • 2020-11-21 12:25

    I know this is an old post but some further explanation might be useful for someone trying to upload multiple files... Here is what you need to do:

    • Input name must be be defined as an array i.e. name="inputName[]"
    • Input element must have multiple="multiple" or just multiple
    • In your PHP file use the syntax "$_FILES['inputName']['param'][index]"
    • Make sure to look for empty file names and paths, the array might contain empty strings. Use array_filter() before count.

    Here is a down and dirty example (showing just relevant code)

    HTML:

    <input name="upload[]" type="file" multiple="multiple" />
    

    PHP:

    //$files = array_filter($_FILES['upload']['name']); //something like that to be used before processing files.
    
    // Count # of uploaded files in array
    $total = count($_FILES['upload']['name']);
    
    // Loop through each file
    for( $i=0 ; $i < $total ; $i++ ) {
    
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
      //Make sure we have a file path
      if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
    
        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
    
          //Handle other code here
    
        }
      }
    }
    

    Hope this helps out!

    0 讨论(0)
  • 2020-11-21 12:27

    Nice link on:

    PHP Single File Uploading with vary basic explanation.

    PHP file uploading with the Validation

    PHP Multiple Files Upload With Validation Click here to download source code

    PHP/jQuery Multiple Files Upload With The ProgressBar And Validation (Click here to download source code)

    How To Upload Files In PHP And Store In MySql Database (Click here to download source code)

    extract($_POST);
        $error=array();
        $extension=array("jpeg","jpg","png","gif");
        foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
                {
                    $file_name=$_FILES["files"]["name"][$key];
                    $file_tmp=$_FILES["files"]["tmp_name"][$key];
                    $ext=pathinfo($file_name,PATHINFO_EXTENSION);
                    if(in_array($ext,$extension))
                    {
                        if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name))
                        {
                            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name);
                        }
                        else
                        {
                            $filename=basename($file_name,$ext);
                            $newFileName=$filename.time().".".$ext;
                            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
                        }
                    }
                    else
                    {
                        array_push($error,"$file_name, ");
                    }
                }
    

    and you must check your HTML code

    <form action="create_photo_gallery.php" method="post" enctype="multipart/form-data">
        <table width="100%">
            <tr>
                <td>Select Photo (one or multiple):</td>
                <td><input type="file" name="files[]" multiple/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td>
            </tr>
        </table>
    </form>
    

    Nice link on:

    PHP Single File Uploading with vary basic explanation.

    PHP file uploading with the Validation

    PHP Multiple Files Upload With Validation Click here to download source code

    PHP/jQuery Multiple Files Upload With The ProgressBar And Validation (Click here to download source code)

    How To Upload Files In PHP And Store In MySql Database (Click here to download source code)

    0 讨论(0)
提交回复
热议问题