Multiple file upload in php

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

    I run foreach loop with error element, look like

     foreach($_FILES['userfile']['error'] as $k=>$v)
     {
        $uploadfile = 'uploads/'. basename($_FILES['userfile']['name'][$k]);
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$k], $uploadfile)) 
        {
            echo "File : ", $_FILES['userfile']['name'][$k] ," is valid, and was                      successfully uploaded.\n";
        }
    
        else 
        {
            echo "Possible file : ", $_FILES['userfile']['name'][$k], " upload attack!\n";
        }   
    
     }
    
    0 讨论(0)
  • 2020-11-21 12:13

    We can easy to upload multiple files using php by using the below script.

    Download Full Source code and preview

    <?php
    if (isset($_POST['submit'])) {
        $j = 0; //Variable for indexing uploaded image 
    
     $target_path = "uploads/"; //Declaring Path for uploaded images
        for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
    
            $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
            $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
            $file_extension = end($ext); //store extensions in the variable
    
      $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
            $j = $j + 1;//increment the number of uploaded images according to the files in array       
    
       if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                    && in_array($file_extension, $validextensions)) {
                if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                    echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
                } else {//if file was not moved.
                    echo $j. ').<span id="error">please try again!.</span><br/><br/>';
                }
            } else {//if file size and file type was incorrect.
                echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
            }
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-11-21 12:17

    It's not that different from uploading one file - $_FILES is an array containing any and all uploaded files.

    There's a chapter in the PHP manual: Uploading multiple files

    If you want to enable multiple file uploads with easy selection on the user's end (selecting multiple files at once instead of filling in upload fields) take a look at SWFUpload. It works differently from a normal file upload form and requires Flash to work, though. SWFUpload was obsoleted along with Flash. Check the other, newer answers for the now-correct approach.

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

    Here is a function I wrote which returns a more understandable $_FILES array.

    function getMultiple_FILES() {
        $_FILE = array();
        foreach($_FILES as $name => $file) {
            foreach($file as $property => $keys) {
                foreach($keys as $key => $value) {
                    $_FILE[$name][$key][$property] = $value;
                }
            }
        }
        return $_FILE;
    }
    
    0 讨论(0)
  • 2020-11-21 12:20

    Simple is that, just count the files array first, then in while loop you can easily do this like

    $count = count($_FILES{'item_file']['name']);
    

    now you got total number of files right.

    In while loop do like this:

    $i = 0;
    while($i<$count)
    {
        Upload one by one like we do normally
        $i++;
    }
    
    0 讨论(0)
  • 2020-11-21 12:22

    this simple script worked for me.

    <?php
    
    foreach($_FILES as $file){
      //echo $file['name']; 
      echo $file['tmp_name'].'</br>'; 
      move_uploaded_file($file['tmp_name'], "./uploads/".$file["name"]);
    }
    
    ?>
    
    0 讨论(0)
提交回复
热议问题