Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

Codeigniter Multiple upload files

Good day Everyone who had an idea on multiple file upload. i want to achieved on how to send multiple file to be inserted in my datatables.

this my view page

                <div class="form-group">
                    <input type="file" name="file">
               
                    <button type="submit" class="btn btn-success">Add New Record</button>
    
                 </div>`enter code here`





this is my function that will upload in the database
           $path = './files';
           $ckdir = is_dir($path);
            if(!$ckdir){
                mkdir($path, 0777, true);
            }
            $userid = $this->session->userdata("sessionid");
            $filename =$path."/".$userid."-".$_FILES["file"]["name"];
            if(!file_exists($filename."pdf")){
              move_uploaded_file($_FILES["file"]["tmp_name"], $filename);                             
            }
            else{
              return false;
            }   
            
question from:https://stackoverflow.com/questions/65911344/codeigniter-multiple-upload-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

View File:

<div class="form-group">
    <input type="file" name="file[]">

    <button type="submit" class="btn btn-success">Add New Record</button>

</div>

Controller File:

$path = './files';
$ckdir = is_dir ( $path );
if (! $ckdir) {
    mkdir ( $path, 0777, true );
}

$countfiles = count ( $_FILES ['files'] ['name'] );

// Looping all files
for($i = 0; $i < $countfiles; $i ++) {
    if (! empty ( $_FILES ['files'] ['name'] [$i] )) {
        
        // Define new $_FILES array - $_FILES['file']
        $_FILES ['file'] ['name'] = $_FILES ['files'] ['name'] [$i];
        $_FILES ['file'] ['type'] = $_FILES ['files'] ['type'] [$i];
        $_FILES ['file'] ['tmp_name'] = $_FILES ['files'] ['tmp_name'] [$i];
        $_FILES ['file'] ['error'] = $_FILES ['files'] ['error'] [$i];
        $_FILES ['file'] ['size'] = $_FILES ['files'] ['size'] [$i];
        
        // Set preference
        $config ['upload_path'] = $path;
        $config ['allowed_types'] = '*';
        $config ['file_name'] = $_FILES ['files'] ['name'] [$i];
        
        // Load upload library
        $this->load->library ( 'upload', $config );
        
        // File upload
        if ($this->upload->do_upload ( 'file' )) {
            // Get data about the file
            $uploadData = $this->upload->data ();
            $filename = $uploadData ['file_name'];
            
            // Initialize array
            $data ['filenames'] [] = $filename;
        }
    }
}

         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...