Multiple file upload in php

前端 未结 14 1634
轮回少年
轮回少年 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:

    
    

    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!

提交回复
热议问题