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

时间:2016-06-06 06:59:42

标签: php mysql codeigniter

我正在使用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']);
		}




当用户提交输入文本以及上传的文件时,功能为undangan:



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




观点:



<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>
&#13;
&#13;
&#13;

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

我的表格:

when dropzone success upload, it hide

showing this

javascript:

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

        });
    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你没有在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
    }

更改控制器

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

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