输入类型文件在CodeIgniter中不起作用

时间:2018-04-13 05:59:52

标签: javascript codeigniter

这是控制器。我更新图像,但我认为输入类型文件不接受文件或不将文件发送到控制器,所以我的代码不能正常工作。我认为控制器代码是真的。

class OurTeam extends CI_Controller {    
public function UpdateTeam()
    { 
        $config['image_library'] = 'gd2';
        $config['upload_path']   = '../employeephoto/';
        $config['source_image']  ='employeephoto/';
        $config['allowed_types']  = 'gif|jpg|png';
        $config['max_size']       = 12024; 
        $config['width']            = 300;
        $config['height']           = 150;

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

        $img = $_FILES['fileupdate']['name'];
        if($img)
        { 

            if (!$this->upload->do_upload('fileupdate'))
            {
                $error = array('error' => $this->upload->display_errors()); 

            }
            else
            {   
                $data = array('upload_data' => $this->upload->data());

                $file_name=($this->upload->data('file_name'));

                $this->load->model('OurTeamModel');

                $id= $_POST['id'];
                $removeimg = $this->OurTeamModel->SelectById($id);
                echo $imgpath = 'employeephoto/'.$removeimg[0]->img;
                if(file_exists($imgpath))
                {
                    unlink($imgpath);
                 }
               else
               {
                   echo "no";
                } 
                  $this->OurTeamModel->UpdateOurTeam($file_name);

                $config['image_library'] = 'gd2';
                $config['upload_path']   = '../employeephoto/'.$file_name;
                $config['source_image']  = '../employeephoto/'.$file_name;
                $config['maintain_ratio'] = false;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['width']         = 660;
                $config['height']        = 300; 

                $this->load->library('image_lib', $config); 
                $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $ok = $this->image_lib->resize(); 

            }

        }
        else
        {
            $this->load->model('OurTeamModel');
            $this->OurTeamModel->UpdateOurTeamRecord();  
        }

            redirect('admin/OurTeam');
    } 
}

这是视图页面

<?php echo form_open_multipart('admin/OurTeam/UpdateTeam');?>

 <input type="file" name="fileupdate" size="20"  id="fileupdate">
 <input type="submit" value="Save" >

</form>

2 个答案:

答案 0 :(得分:0)

请改为         从:

$config['image_library'] = 'gd2';
    $config['upload_path']   = '../employeephoto/';
    $config['source_image']  ='employeephoto/';
    $config['allowed_types']  = 'gif|jpg|png';
    $config['max_size']       = 12024; 
    $config['width']            = 300;
    $config['height']           = 150;

TO:

$config['upload_path'] = './employeephoto/';
    $config['allowed_types'] = 'gif|jpg|png|doc|txt';
    $config['max_size'] = 1024 * 8;
    $config['encrypt_name'] = TRUE;

我检查了你的代码。请做一件事这篇文章并一步一步地说。希望这是有道理的。

https://www.tutorialspoint.com/codeigniter/codeigniter_file_uploading.htm

以下是如何在CodeIgniter中上传文件的逐步说明。

答案 1 :(得分:0)

试试这个并让我知道输出的是什么(可能需要一个printsrc并上传到imgur):

public function UpdateTeam() {
    $config['image_library'] = 'gd2';
    $config['upload_path'] = '../employeephoto/';
    //$config['source_image'] = 'employeephoto/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 12024;
    $config['width'] = 300;
    $config['height'] = 150;

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

    if (!empty($_FILES['fileupdate']['name'])) {

        if (!$this->upload->do_upload('fileupdate')) {
            echo 'upload failed with error: <br>';
            exit($this->upload->display_errors());
        } else {
            echo 'upload passed <br>';

            $file_name = ($this->upload->data('file_name'));

            $this->load->model('OurTeamModel');

            $id = $_POST['id'];
            $removeimg = $this->OurTeamModel->SelectById($id);
            echo $imgpath = 'employeephoto/' . $removeimg[0]->img;
            if (file_exists($imgpath)) {
                unlink($imgpath);
            } else {
                echo "no";
            }
            $this->OurTeamModel->UpdateOurTeam($file_name);

            echo 'resizing image <br>';

            $config['image_library'] = 'gd2';
            $config['upload_path'] = '../employeephoto/' . $file_name;
            $config['source_image'] = '../employeephoto/' . $file_name;
            $config['maintain_ratio'] = false;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['width'] = 660;
            $config['height'] = 300;

            $this->load->library('image_lib', $config);
            $this->image_lib->clear();
            $this->image_lib->initialize($config);
            if (!$this->image_lib->resize()) {
                echo 'image resize failed <br>';
                exit($this->image_lib->display_errors());
            }
            echo 'all good <br>';
        }
    } else {
        echo 'no file <br>';
        $this->load->model('OurTeamModel');
        $this->OurTeamModel->UpdateOurTeamRecord();
    }

    //redirect('admin/OurTeam');
}