gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:29:32 25 4
gpt4 key购买 nike

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

这是我的 Controller :

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>

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

最佳答案

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

        // 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
{
if(move_uploaded_file($_FILES["file"]["tmp_name"], "kirim_undangan/" . $newfilename)) {
// Set on upload success
setcookie("fileupload", $newfilename);
echo "File uploaded successfully.";
}
}

稍后检索:

$uploadImg = $_COOKIE["fileupload"];

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

关于php - 为上传的文件名和mysql数据库生成相同的uniqid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com