Home » Php » javascript – Return Dynamic Id to Upload FIles

javascript – Return Dynamic Id to Upload FIles

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a system that works with an account holder and dependent.

For each holder, I can have n dependents.

For each member, I can upload x documents, which are attached to a folder with a document number inserted and later zipped .

For the holder without a dependent, I can use this function here

jQuery

   var i = 0;
$(document).ready(function(){


        var sDiv = $('#hDiv');
        var i = $('#hDiv p').length + 1;

        $('#addDiv').on('click', function() {
                $('<p><label for="hInputs"><input type="file" id="hInput" size="20" name="hInput_' + i +'" value="" placeholder="Input Value" multiple/></label> <a href="#" id="remScnt">Apaga</a></p>').appendTo(sDiv);
                i++;
                return false;
        });


});

AJAX

$.each($("input[type=file]"), function(i, obj) 
            {
                $.each(obj.files,function(j, file)
                {
                    form_data.append('hInput[]', file, file.name);
                })
            });

PHP

$cpf = $_POST['cpf'];

    $c = '0';
    $total      = count($_FILES['hInput']['name']); 

    $dir        = "zip/".$cpf;                  

    if (is_dir($dir))                               
    {
        $di     = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
        $ri     = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);

        foreach ( $ri as $file ) 
        {
            $file->isDir() ?  rmdir($file) : unlink($file); 
        }       
        rmdir($dir);                    
        mkdir($dir);                    
    }
        else
        {
            mkdir($dir);                
        }


    for( $i=0 ; $i < $total ; $i++ )    
    {

        $size = 1 * 1024 * 1024;        
        $tmpFilePath = $_FILES['hInput']['tmp_name'][$i];
        $tmpFileSize = $_FILES['hInput']['size'][$i];

        if ($tmpFileSize > $size)
        {

            //error
        }

        $newFilePath = "./zipados/".$cpf."/". $_FILES['hInput']['name'][$i];

        if(move_uploaded_file($tmpFilePath, $newFilePath)) 
        {
            //x
        }
            else
            {
                //x
            }
    }

    $zip = new Zip();
        $zip->zip_start($dir.".zip");   
        $zip->zip_add("zipados/".$cpf); 
    $zip->zip_end();    

How can i do this with dependents using the same function and store in server like
zip >> DocHolder >>> DEP1 >>> DEP2

How to&Answers: