关于数据库的Codeigniter文件上传错误

时间:2017-01-07 06:31:01

标签: php mysql codeigniter

您好先生我已经使用codeigniter完成了上传文件的表单。但是它显示了一个错误。

这是我的控制器页面:Admin.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array(
            'form',
            'url',
            'file'
        ));
        $this->load->library(array(
            'session',
            'form_validation',
            'email'
        ));
        $this->load->database();
        $this->load->model('user_model');
    }

    function index()
    {
        $this->load->view('admin/index');
    }

    function addcourse()
    {
        $this->load->view('admin/addcourse');
    }

    function submitcourse()
    {

        // $this->load->helper('file');

        $this->form_validation->set_rules('coursename', 'Course Name', 'trim|required'); // trim removes whitespace from the beginning and end of the string
        $this->form_validation->set_rules('duration', 'Duration', 'trim|required');
        if (empty($_FILES['curriculam']['name'])) {
            $this->form_validation->set_rules('curriculam', 'Curriculam', 'required|trim', array(
                'required' => 'Upload Curriculam'
            ));
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = 1000;

            // $config['max_width']     = 1024;
            // $config['max_height']    = 768;

            $this->load->library('upload', $config);
        }

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('admin/addcourse');
        }
        else {
            $data = array(
                'course_name' => $this->input->post('coursename') ,
                'duration' => $this->input->post('duration') ,
                'course_desc' => $this->input->post('desc') ,
                'curriculam' => $this->input->post('curriculam')
            );
            if ($this->user_model->insertCourses($data)) {
                $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Course Uploaded</div>');
                redirect('admin/addcourse');
            }
            else {

                // error

                $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('admin/addcourse');
            }
        }
    }
}
?>

查看页面:Addcourse.php

<div class="row">
   <div class="col-md-12 col-sm-12 col-xs-12">
      <div class="x_panel">
         <div class="x_title">
            <h2>Add a New Course </h2>
            <div class="clearfix"></div>
         </div>
         <div class="x_content">
            <?php echo $this->session->flashdata('msg'); ?>
            <br />
            <?php echo form_open_multipart('admin/submitcourse', array('id' =>'demo-form2', 'data-parsley-validate class' =>'form-horizontal form-label-left'));?>
            <div class="form-group">
               <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Course Name *
               </label>
               <div class="col-md-6 col-sm-6 col-xs-12">
                  <input type="text" id="coursename" name="coursename" class="form-control col-md-7 col-xs-12" value="<?php echo set_value('coursename');?>" >
                  <span class="text-danger"><?php echo form_error('coursename'); ?></span>
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Duration *
               </label>
               <div class="col-md-6 col-sm-6 col-xs-12">
                  <input type="text" id="duration" name="duration" class="form-control col-md-7 col-xs-12" value="<?php echo set_value('duration');?>" >
                  <span class="text-danger"><?php echo form_error('duration'); ?></span>
               </div>
            </div>
            <div class="form-group">
               <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Short Description</label>
               <div class="col-md-6 col-sm-6 col-xs-12">
                  <textarea class="form-control" rows="3" name="desc" value="<?php echo set_value('desc');?>" ></textarea>
               </div>
            </div>
            <div class="form-group">
               <label class="control-label col-md-3 col-sm-3 col-xs-12">Upload Curriculam
               </label>
               <div class="col-md-6 col-sm-6 col-xs-12">
                  <input id="curriculam"  type="file" name="curriculam" >
                  <span class="text-danger"><?php echo form_error('curriculam'); ?></span>
               </div>
            </div>
            <div class="ln_solid"></div>
            <div class="form-group">
               <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                  <button type="submit" class="btn btn-primary">Cancel</button>
                  <?php echo form_submit('submit','Submit', array('class' => 'btn btn-success')) ;?>
                  <!--<button type="submit" class="btn btn-success">Submit</button>-->
               </div>
            </div>
            <?php echo form_close(); ?>
         </div>
      </div>
   </div>
</div>

但是它显示了关于文件上传的错误。所以我需要帮助上传我的pdf并将文件名发送到数据库。

1 个答案:

答案 0 :(得分:0)

之后的控制器中
 $this->load->library('upload', $config);

添加以下代码

$target_dir = "uploads/";
$target_file = $target_dir . time().basename($_FILES["curriculam"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$file_name = time().basename($_FILES["curriculam"]["name"]);
move_uploaded_file($_FILES["curriculam"]["tmp_name"], $target_file);

然后改变

 'curriculam' => $file_name