0

I have been digging for an answer for many days. how do i upload multiple file to the newly created directory. If you look into file_upload.php you will find two $upload_dir variable. So, let's call first $upload_dir as direct folder and Second, $upload_dir as make dir. Simple

So, When I select first $upload_dir it does upload all files directly into the folder and When I select second $upload_dir what it does is create a random folder but unable to upload any file.

I want to upload multiple file into newly created folder

I did refer this PHP - Upload multiple photos into newly created Directory and Multiple file upload and store them in a directory but didn't work for me

index.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>  

file_upload.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."; 
    } 
}  
  
?> 

Please Help!

Thank you in Advance

9
  • mkdir returns a boolean so assigning $upload_dir=mkdir(.....) is probably NOT what you want Commented Feb 23, 2021 at 16:53
  • dump the $_FILES array to see how it's structured... then fix your code according to the true structure... honestly, I wonder how people want to program if they don't KNOW how their data looks like... Commented Feb 23, 2021 at 16:53
  • FYI - the 3rd argument to mkdir is worth using as it'll ensure the generation of all folders in the path which can be useful Commented Feb 23, 2021 at 16:57
  • @LarsStegelitz I have structured $_FILES accordingly then too unable to solve my issue Commented Feb 23, 2021 at 17:33
  • @ProfessorAbronsius Sorry Sir. I am unable to connect with your answer. If possible can you provide me some more details on the question. Commented Feb 23, 2021 at 17:36

1 Answer 1

1

mkdir returns a boolean (true or false), not the created directory path.

You probably want to define the path in $upload_dir but not assign the result of the mkdir to it:

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

if (mkdir($upload_dir, 0777)) {
   //process images
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.