0

我一直在寻找答案很多天。如何将多个文件上传到新创建的目录。如果您查看file_upload.php,您会发现两个$upload_dir变量。所以,让我们首先调用$upload_dir直接文件夹,然后调用$upload_dirmake dir。简单的

所以,当我第一次选择时$upload_dir,它确实将所有文件直接上传到文件夹中,当我选择第二次时$upload_dir,它所做的是创建一个随机文件夹但无法上传任何文件。

我想将多个文件上传到新创建的文件夹中

我确实参考了这个PHP - 将多张照片上传到新创建的目录多个文件上传并将它们存储在一个目录中,但对我不起作用

索引.php

<form action="file_upload.php" method="POST"
            enctype="multipart/form-data"> 
  
        <h2>Upload Files</h2> 
          
        <p> 
            Select files to upload:  
              
            <!-- name of the input fields are going to 
                be used in our php script-->
            <input type="file" name="files[]" multiple> 
              
            <br><br> 
              
            <input type="submit" name="submit" value="Upload" > 
        </p> 
    </form>  

文件上传.php

<?php  
  
// session_start();
// Check if form was submited 
if(isset($_POST['submit'])) { 
  
    // Configure upload directory and allowed file types 
    $rand_name = rand(1, 10000);
    // $upload_dir = 'C:/fileUpload/'.DIRECTORY_SEPARATOR;
    // $permit = 0777;
    $allowed_types = array('jpg', 'png', 'jpeg', 'gif'); 
    $upload_dir = mkdir('C:/fileUpload/'. $rand_name .'/'.DIRECTORY_SEPARATOR, 0777);
      
    // Define maxsize for files i.e 10MB 
    $maxsize = 10 * 1024 * 1024;  
  
    // Checks if user sent an empty form  
    if(!empty(array_filter($_FILES['files']['name']))) { 
  
        // Loop through each file in files[] array 
        foreach ($_FILES['files']['tmp_name'] as $key => $value) { 
              
            $file_tmpname = $_FILES['files']['tmp_name'][$key]; 
            $file_name = $_FILES['files']['name'][$key]; 
            $file_size = $_FILES['files']['size'][$key]; 
            $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); 
  
            // Set upload file path 
            $filepath = $upload_dir.$file_name;
  
            // Check file type is allowed or not 
            if(in_array(strtolower($file_ext), $allowed_types)) { 
  
                // Verify file size - 10MB max  
                if ($file_size > $maxsize)          
                    echo "Error: File size is larger than the allowed limit.";  
  
                // If file with name already exist then append time in 
                // front of name of the file to avoid overwriting of file 
                if(file_exists($filepath)) { 
                    $filepath = $upload_dir.time().$file_name; 
                      
                    if( move_uploaded_file($file_tmpname, $filepath)) { 
                        echo "{$file_name} successfully uploaded <br />"; 
                    }  
                    else {                      
                        echo "Error uploading {$file_name} <br />";  
                    } 
                } 
                else { 
                  
                    if( move_uploaded_file($file_tmpname, $filepath)) { 
                        echo "{$file_name} successfully uploaded <br />"; 
                    } 
                    else {                      
                        echo "Error uploading {$file_name} <br />";  
                    } 
                } 
            } 
            else { 
                  
                // If file extention not valid 
                echo "Error uploading {$file_name} ";  
                echo "({$file_ext} file type is not allowed)<br / >"; 
            }  
        } 
    }  
    else { 
          
        // If no files selected 
        echo "No files selected."; 
    } 
}  
  
?> 

请帮忙!

先感谢您

4

1 回答 1

1

mkdir返回一个布尔值(真或假),而不是创建的目录路径。

您可能想要定义路径$upload_dir但不将 mkdir 的结果分配给它:

$upload_dir = 'C:/fileUpload/'. $rand_name .'/';

if (mkdir($upload_dir, 0777)) {
   //process images
}
于 2021-02-23T16:53:42.873 回答