为上传的文件名和mysql数据库生成相同的uniqid

时间:2016-06-06 03:44:10

标签: php mysql codeigniter

我有一个允许访客用户上传xls文件的页面。如何确定该文件是否归该用户所有?我将上传的文件重命名为uniqid(),并且在插入数据库时​​也存储了uniqid(),但问题是,来自数据库的uniqid()uniqid()文件名是不同的。 对于这种情况,有没有人有最好的方法?因为我不希望用户先登录上传文件。

这是我的控制人员:

function upload() 
    {
        $filename = $_FILES["file"]["name"];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
        $file_ext = substr($filename, strripos($filename, '.')); // get file name
        $filesize = $_FILES["file"]["size"];
        $allowed_file_types = array('.doc','.docx','.xls','.xlsx'); 

        if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
        {   
            // Rename file to uniqid()
            $newfilename = uniqid($filename) . $file_ext;
            if (file_exists("upload/" . $newfilename))
            {
                // file already exists error
                echo "You have already uploaded this file.";
            }
            else
            {       
                move_uploaded_file($_FILES["file"]["tmp_name"], "kirim_undangan/" . $newfilename);
                echo "File uploaded successfully.";     
            }
        }
    }

这是插入数据库时​​的代码:

function undangan()
    {
            $email          =   $this->input->post('email');
            $from_nama      =   $this->input->post('from_nama');
            $from_phone     =   $this->input->post('from_phone');
            $data_user = array(

                'email' => $email,
                'name'  => $from_nama,
                'phone' => $from_phone,
                'status'=> '0',
                'unique_id'=> uniqid() //here is the uniqid that upload to database
                );

            $this->load->model('excel');
            $this->excel->tambahuser($data_user); //sending data_user only
            $data['msg'] = "Terima kasih ! Silahkan tunggu konfirmasi biaya melalui email !";

            $this->load->library('email_ses');
            $this->email_ses->send();

        $data = json_encode(array("email" => $email, "from_nama" => $from_nama,"from_phone" => $from_phone ));
        $this->load->view('kirimundangan.php',$data);
    }

表格输入:

<div id="form_pesan">
   <div action="<?php echo site_url('/kirim/upload'); ?>" class="dropzone" id="dropzone_form">
       <div class="dz-message" data-dz-message><span><h4>Click or drop file here</h4></span></div>
            </div>
       <div class="row">
          <div class="alert alert-danger" id="alert2" style="display: none;">Upload fail !
          </div>
          <div class="alert alert-info" id="alert_drpzone" style="display: none;">Success !
          </div>
       </div>

任何帮助将不胜感激..谢谢

1 个答案:

答案 0 :(得分:1)

由于您不希望用户登录,我能想到的唯一方法是使用COOKIE:http://php.net/manual/en/function.setcookie.php

$uploadImg = $_COOKIE["fileupload"];

稍后检索:

phpmyadmin

但请注意,用户可以关闭Cookie,因此您需要提醒用户必须启用Cookie。