简体   繁体   中英

multiple file upload in php not working on my server

i want to multiple file upload in php.. but its not working here is my code. and link

<form action="" method="POST" enctype="multipart/form-data">  
<input type="file" name="files[]" multiple/>  
<input type="submit"/>

Link

    <?php    
      if(isset($_FILES['files']))  
    {  
        $errors= array();  
        foreach($_FILES['files']['tmp_name'] as $key => $tmp_name )
            {  
            $file_name = $key.$_FILES['files']['name'][$key];  
            $file_size =$_FILES['files']['size'][$key];  
            $file_tmp =$_FILES['files']['tmp_name'][$key];  
            $file_type=$_FILES['files']['type'][$key];      
            if($file_size > 2097152){  
            $errors[]='File size must be less than 2 MB';  
            }       
             $desired_dir="uploads";  
             if(empty($errors)==true)
             {  
                if(is_dir($desired_dir)==false)
                  {  
                    mkdir("$desired_dir", 0777);  
                  }  
                if(is_dir("$desired_dir/".$file_name)==false)
                     {  
                    move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                    }else
                    {  
                    $new_dir="$desired_dir/".$file_name.time();  
                     rename($file_tmp,$new_dir) ;               
                     }  

              }else{  
            }  
         }
         if(empty($error)){  
            echo "Success";  
        }  }
?>

here is the upload code. when i am select multiple file to upload than no any response from server you can see live on my given link.

Use like

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple/>
    <input type="submit"/>
</form>

<?php
$target_dir = "uploads/";
if(isset($_POST))
{
    if(isset($_FILES["files"]["name"]) && is_array($_FILES["files"]["name"]) && $_FILES["files"]["name"]!= false)
    {       
        foreach($_FILES["files"]["name"] as $key=>$name)
        {
            $target_file = $target_dir . basename($name);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
             if (move_uploaded_file($_FILES["files"]["tmp_name"][$key], $target_file)) {
                echo  $_FILES["files"]["name"][$key] . " uploaded <br/>";                
             }
        }
    }

} ?>

If you are uploading big files then please check your php.ini settings , check https://doc.owncloud.org/server/8.0/admin_manual/configuration_files/big_file_upload_configuration.html

This works for me.

  • Make sure your uploads directory is writeable
  • You have to create a loop for each files

upload.php

<?php
    // Count # of uploaded files in array
    $total = count($_FILES['files']['name']);

    // Loop through each file
    for($i=0; $i<$total; $i++) {
      $tmpFilePath = $_FILES['files']['tmp_name'][$i];

      if ($tmpFilePath != ""){
        $newFilePath = "uploads/" . $_FILES['files']['name'][$i];

        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
            echo 'Upload success!';
        }
      }
    }
?>
<form action="" method="POST" enctype="multipart/form-data">  
    <input type="file" name="files[]" multiple/>  
    <input type="submit"/>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM