Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call mkdir() once in method of controller

I have a form which uploads multiple files. No of files uploaded once are uploaded in a unique directory. The problem I am facing right now is that For one user I want to run the mkdir() once. Don't know how to do it exactly.

Controller.php

class Upload_file extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }
    function upload_it()
    {
        $this->load->helper('form');
        $uniqueID = uniqid(); 
        mkdir("application/uploads/".$uniqueID);

        $config['upload_path'] = 'application/uploads/'.$uniqueID;
        $config['allowed_types'] = '*';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->upload->set_allowed_types('*');

        $data['upload_data'] = '';
        if (!$this->upload->do_multi_upload('files')) {
            $data = array('msg' => $this->upload->display_errors());

        } 
        else
        {
              $data['upload_data'] = $this->upload->get_multi_upload_data();
        }
        $this->load->vars($data);

        $this->load->view('upload');
    }
}

HTML

<form target="upload_target" id="fileupload" method="post" action="<?php echo site_url('upload_file/upload_it'); ?>" enctype="multipart/form-data">

<div class="fileUpload btn btn-warning">
    <span>Browse</span>
    <input id="uploadBtn" type="file" multiple="multiple" name="files[]" class="upload" />
</div>
    <input id="uploadFile" style="width: 160px; margin-top: 30px;float: left;height: 35px;" placeholder="Choose File" disabled="disabled" />
    <button id="btnupload" style="padding: 4.5px; float:left;margin-top: 30px;border-radius: 0px;" disabled="disabled" type="submit" class="btn btn-primary btn-lg"><span class="glyphicon glyphicon-upload"></span></button>

</form> 
like image 516
BinteHava Avatar asked Mar 31 '14 06:03

BinteHava


2 Answers

I dont know if I understood your problem, but I guess you should just check if the folder exists first, then create it:

if (!file_exists("application/uploads/".$uniqueID)) {
    mkdir("application/uploads/".$uniqueID);
}
like image 194
Fadey Avatar answered Sep 30 '22 01:09

Fadey


You can make sure first either folder already exit or not, and until check with the new folder name

Example:- Create a new filed in users table, having name folder name, and first time if folder column is empty then create a new one as given in blow:

$folder_path = "application/uploads/".$uniqueID;
while (file_exists($folder_pat)) {
    $uniqueID = $this->uniqid();   // get the next unique id
    $folder_path = "application/uploads/".$uniqueID;
}

mkdir($folder_path);

once folder is created, then when ever you want to upload any file againt that user then first check in db either it' already exist or not, if not exit then create a new dir. else use the previous one that already created.

like image 23
Suleman Ahmad Avatar answered Sep 30 '22 02:09

Suleman Ahmad