【问题标题】:My file type not saving on my database我的文件类型没有保存在我的数据库中
【发布时间】:2015-06-07 19:00:34
【问题描述】:

我已完成文件上传,但如果文件名为“213134.jpg”但我的代码存储数据库仅名称“213134”而没有文件类型。你能帮我么。我已使用 CI 完成此任务

function postMessage(){
    $config['upload_path'] = './uploads/inbox/';
    $config['allowed_types'] = 'jpg';
    $config['max_size'] = '2048';
    $config['max_width']  = '3000';
    $config['max_height']  = '3000';
    $random=rand(00000, 99999);
    $id=$this->session->userdata('id');
    $pic=$id*$random;
    $config['file_name'] =$pic;

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        }
    else
    {
        $blog_msg ='Sorry, Picture uploaded.';
     }

    $u_id=$this->session->userdata('id');
    $u_name=$this->session->userdata('u_fname');
    $to=$this->uri->segment(3);
    $date= $date=date('d-M-Y');
    $message=$_POST["message"];

    $data=array(
        'm_from'=>$u_id,
        'from_name'=>$u_name,
        'm_date'=>$date,
        'm_body'=>$message,
        'm_to'=>$to,
        'm_attach'=>$pic 
        );
}

【问题讨论】:

  • 你的数据库代码在哪里?
  • 请修复代码标记。
  • $inputed=$this->db->insert($this->message,$data); $insert_id = $this->db->insert_id(); $this->db->trans_complete();返回 $insert_id;
  • $pic=$id*$random; 应该是这样的$pic=$id*$random.'.jpg';
  • 类型肯定是.jpg而不是.jpeg?如果 .jpg 使用 'allowed_types' => 'jpg|jpeg'

标签: codeigniter file-upload


【解决方案1】:
$config['allowed_types'] = 'jpg|jpeg';//Change this

方法01

$data=array(
        'm_from'=>$u_id,
        'from_name'=>$u_name,
        'm_date'=>$date,
        'm_body'=>$message,
        'm_to'=>$to,
        'm_attach'=>$this->input->post('pic'), //change this(in your HTML attribute name='' )
        );

方法02

否则你可以试试

$ext = pathinfo($pic, PATHINFO_EXTENSION); //Get the Extension

$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random.$ext;//change this
$config['file_name'] =$pic;

【讨论】:

猜你喜欢
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 2021-05-21
  • 2021-02-24
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多