在调用上传功能之前创建唯一ID,并始终为一个人传递它

时间:2014-03-31 09:28:36

标签: php codeigniter

我有一个注册表单,用户可以在其中上传多个文件。他可以一次上传十个文件,每个一个一个。现在我想要的是在调用上传函数之前创建uniqueId并将其传递给mkdir()的上传函数。 现在我在上传功能中创建了唯一的ID,但实际上并不想这样。

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


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');
}

1 个答案:

答案 0 :(得分:0)

$uniqueID = uniqid(); 
mkdir("application/uploads/".$uniqueID);

function upload_it($upload_path)

{
    $this->load->helper('form');

    $config['upload_path'] = $upload_path;
    $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');
}