gpt4 book ai didi

php - 将上传的文件路径存储到数据库mysql并下载

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

我正在使用 codeigniter,现在我想将文件路径上传到表中。下面的代码仅存储文件夹路径,例如“
http://localhost/kirimundangan.com/kirim_undangan/ ' 不是实际的文件名。我期望像'http://localhost/kirimundangan.com/kirim_undangan/somefile.xlsx '.

用户通过dropzone上传文件时的上传功能:

function upload() 
{
$this->load->library('session');

if (!empty($_FILES))
{
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name

$tempFile = $_FILES['file']['tmp_name'];
$data = uniqid(). $file_ext;
$targetPath = getcwd() . '/kirim_undangan/';
$targetFile = $targetPath . $data ;

move_uploaded_file($tempFile, $targetFile);
$_SESSION["xls"] = $data;
print_r($_SESSION['xls']);
}

当用户提交输入文本和上传的文件时执行此功能:

function undangan()
{$email = $this->input->post('email');
$from_nama = $this->input->post('from_nama');
$from_phone = $this->input->post('from_phone');
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name

$tempFile = $_FILES['file']['tmp_name'];
$data_file = uniqid(). $file_ext;
$targetPath = getcwd() . '/kirim_undangan/';
$targetFile = $targetPath . $data ;

$data_user = array(

'email' => $email,
'name' => $from_nama,
'phone' => $from_phone,
'status' => '0',
'filename_user' => site_url('/kirim_undangan/.uniqid()'),

);
$this->load->model('excel');
$this->excel->tambahuser($data_user);

型号:

 function tambahuser($data_user)
{
$this->db->insert('request', $data_user);
$this->db->insert_id();

foreach ($data_user as $key)
{
$data = array(
'from_name' => $this->input->post('from_nama'),
'from_phone' => $this->input->post('from_phone')
);
}

}

以及 View :

<div class="modal-body">

<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>Klik atau drop file Disini
</h4></span></div>
</div>
<div class="row" id="form_user" style="display: none;">

<h4 id="form">Data Personal</h4>

<div class="col-sm-4">
<input type="email" class="form-control input-lg" id="email" name="email" placeholder="Email" required>
</div>

<div class="col-sm-4">
<input type="text" class="form-control input-lg" id="from_nama" name="from_nama" placeholder="Nama" required>
</div>

<div class="col-sm-4">
<input type="number" class="form-control input-lg" id="from_phone" name="from_phone" placeholder="Phone" required>
</div>
<div>
<input type="hidden" name="id" id="id" />
</div>
<br>
<div class="row" align="center">
<button id="pesan" type="button" class="btn btn-download btn-md" onclick=pesan()>
<span class="glyphicon glyphicon-send" aria-hidden="true" ></span>Pesan
</button>
</div>

</div>

如果路径存储在表中,我将使用它来下载文件。顺便说一句,将文件路径存储到数据库中是否比存储实际文件更好? (我需要存储xls或doc文件)

我的表格:

when dropzone success upload, it hide

showing this

JavaScript:

Dropzone.autoDiscover = false;
var myDropzone = new Dropzone('#dropzone_form',{
acceptedFiles: ".xlsx, .xls, .docs, .doc, .pdf" ,
"maxFiles":1 ,
init: function () {
var thisDropzone = this;

this.on("success", function(files, response) {
$('#alert_drpzone').show();
$('#form_user').show();
$('#dropzone_form').hide();
$('#myModalLabel').hide();
});

this.on("error", function(files,response) {
$('#alert2').show();
})
}
});


function pesan()
{
email = $("#email").val();
from_nama = $("#from_nama").val();
from_phone = $("#from_phone").val();

$.ajax
({
url : "<?php echo site_url('kirim/undangan')?>/",
type: "POST",
dataType: "text",
data:{from_nama: from_nama, email: email, from_phone: from_phone},
success: function(data)
{
$('#alert_sukses').show();
$('#form_pesan').hide();
$('#myModalLabel').hide();
$('#email'+data).html(data.email);
$('#from_nama'+data).html(data.from_nama);
$('#from_phone'+data).html(data.from_phone);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error upload data');
}

});
}

最佳答案

您没有在undangan()中调用upload()。在 upload() 中返回 $targetFile,以便它为您提供上传的文件名,然后将该名称添加到 $data_user 中。

function upload() 
{
if (!empty($_FILES))
{
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name

$tempFile = $_FILES['file']['tmp_name'];
$data = uniqid(). $file_ext;
$targetPath = getcwd() . '/kirim_undangan/';
$targetFile = $targetPath . $data ;

move_uploaded_file($tempFile, $targetFile);
return $targetFile; //return uploaded file name with directory
}

更改 Controller ,例如

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

$data_user = array(
'email' => $email,
'name' => $from_nama,
'phone' => $from_phone,
'status' => '0',
'filename_user' => $uploadedFileName,

);
$this->load->model('excel');
$this->excel->tambahuser($data_user);
}

希望这能解决您的问题。如果您需要任何帮助。我很高兴为您提供指导

关于php - 将上传的文件路径存储到数据库mysql并下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651288/

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