无法上传图片codeigniter没有错误

时间:2016-10-06 07:36:34

标签: codeigniter

上传图片文件时遇到问题。

这是我的视图文件内容

<form action="<?php base_url()."profile/upload"?>" method="POST" enctype="multipart/form-data" >

    <input type="hidden" name="id_akun" value="<?php echo $query->id_akun?>"size="20" />
    <input type="file" name="userfile" required>
    <input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>

这是我的控制器(个人资料)

public function upload() {
    $config = array(
        'upload_path'     => dirname($_SERVER["SCRIPT_FILENAME"])."/upload/",
        'upload_url'      => base_url()."upload/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf",
        'overwrite' => false,
        'max_size' => "2048000", 
        'max_height' => "768",
        'max_width' => "1024"
    );

    $this->load->library('upload', $this->config);
    if($this->upload->do_upload()) {
        echo "file upload success";
    } else {
        echo "file upload failed";
    }

}

当我尝试上传时,图片没有上传,并且没有显示错误。我的地址栏中的网址如下所示 http://localhost/mataramtest/profile?id_akun=6&userfile=1.PNG&submit=Upload

2 个答案:

答案 0 :(得分:0)

您的代码有几个问题。修改并在下面给出

 public function upload()
{
    $config = array(
        'upload_path'     => "./upload/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf",
        'overwrite' => false,
        'max_size' => "2048000",
        'max_height' => "768",
        'max_width' => "1024"
        );
    $this->load->library('upload', $config);
    if($this->upload->do_upload('userfile'))
    {
        echo "file upload success";
    }
    else
    {
        echo "file upload failed";
    }
}

在视图文件

<form action="<?php echo site_url("profile/upload");?>" method="POST" enctype="multipart/form-data" >
  <!--rest of the code is ok-->

答案 1 :(得分:0)

public function img_upload()
{
    $config = array(
        'upload_path' => "uploads",
        'allowed_types' => "*",
        'overwrite' => TRUE,
        'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        'max_height' => "3000",
        'max_width' => "3000"
    );
    $this->upload->initialize($config);
    $this->load->library('upload', $config);

    if($this->upload->do_upload()) {
        $response   =    array('upload_data' => $this->upload->data());
    }
    else{
        $error              =   array('error'=>$this->upload->display_errors());
    print_r($error); 

    }

}