如何使用codeigniter上传图片?

时间:2017-02-09 08:35:57

标签: php codeigniter

我可以设法将数据插入到tblaccount中。但问题是图片无法上传。 tblaccount包含firstname,lastname,email,department,username和password的正确数据,但即使我上传了一些图片,图片仍然是空白的。

更新:图片现在可以上传到文件夹中,但tblaccount没有运气。它只显示空白数据。

signup.php控制器:

public function index()
{
    // set form validation rules
    $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]');
    $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]');
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[tblaccount.Email]');
    $this->form_validation->set_rules('department', 'Department', 'trim|required|alpha|min_length[3]|max_length[30]');
    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[30]|is_unique[tblaccount.Username]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');
    $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]');
    $this->form_validation->set_rules('picture', 'Image', 'trim|required');

    // submit
    if ($this->form_validation->run() == FALSE)
    {
        // fails
        $this->load->view('signup_view');
    }
    else
    {
        if(!empty($_FILES['picture']['name']))
        {
            $config['upload_path'] = './uploads/images/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = 10000000;
            $config['file_name'] = $_FILES['picture']['name'];

            //Load upload library and initialize configuration
            $this->load->library('upload',$config);
            $this->upload->initialize($config);

            if($this->upload->do_upload('picture'))
            {
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
            }
            else
            {
                $picture = '';
                $error = array('error' => $this->upload->display_errors());
                echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '".base_url("index.php/signup")."';</script>";
            }
        }
        else
        {
            $picture = '';
        }
            $data = array(
                'First_Name' => $this->input->post('firstname'),
                'Last_Name' => $this->input->post('lastname'),
                'Email' => $this->input->post('email'),
                'Department' => $this->input->post('department'),
                'Username' => $this->input->post('username'),
                'Password' => $this->input->post('password'),
                'Picture' => $picture
            );

            if ($this->account_model->insert($data))
            {
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>');
                redirect('login');
            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('signup');
            }
    }
}

signup.php查看:

<div class="row">
        <div class="col-md-4 col-md-offset-4 well">
            <?php echo form_open_multipart('signup');?>
                <legend>Signup</legend>

                    <div class="form-group">
                        <label for="name">First Name</label>
                            <input class="form-control" name="firstname" placeholder="First Name" type="text" value="<?php echo set_value('First_Name');?>"/>
                            <span class="text-danger"><?php echo form_error('firstname'); ?></span>
                    </div>          

                    <div class="form-group">
                        <label for="name">Last Name</label>
                            <input class="form-control" name="lastname" placeholder="Last Name" type="text" value="<?php echo set_value('Last_Name');?>"/>
                            <span class="text-danger"><?php echo form_error('lastname'); ?></span>
                    </div>

                    <div class="form-group">
                        <label for="email">Email Address</label>
                            <input class="form-control" name="email" placeholder="Email Address" type="text" value="<?php echo set_value('Email');?>"/>
                            <span class="text-danger"><?php echo form_error('email'); ?></span>
                    </div>

                    <div class="form-group">
                        <label for="email">Department</label>
                            <input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
                            <span class="text-danger"><?php echo form_error('department'); ?></span>
                    </div>

                    <div class="form-group">
                        <label for="email">Username</label>
                            <input class="form-control" name="username" placeholder="Username" type="text" value="<?php echo set_value('Username');?>"/>
                            <span class="text-danger"><?php echo form_error('username'); ?></span>
                    </div>

                    <div class="form-group">
                        <label for="subject">Password</label>
                            <input class="form-control" name="password" placeholder="Password" type="password"/>
                            <span class="text-danger"><?php echo form_error('password'); ?></span>
                    </div>

                    <div class="form-group">
                        <label for="subject">Confirm Password</label>
                            <input class="form-control" name="cpassword" placeholder="Confirm Password" type="password"/>
                            <span class="text-danger"><?php echo form_error('cpassword'); ?></span>
                    </div>

                    <div class="form-group">
                        <label for="subject">Profile Picture:</label>
                            <input class="form-control" name="picture" accept="image/*" type="file"/>
                            <span class="text-danger"><?php echo form_error('picture'); ?></span>
                    </div>

                    <div class="form-group">
                        <button name="submit" type="submit" class="btn btn-info">Signup</button>
                        <button name="cancel" type="reset" class="btn btn-info">Cancel</button>
                    </div>

            <?php echo form_close(); ?>
        </div>
    </div>

2 个答案:

答案 0 :(得分:0)

在您的上传路径

$config['upload_path'] = 'uploads/images/';

尝试

$config['upload_path'] = './uploads/images/';
$config['upload_path'] = FCPATH . '/uploads/images/';

我使用form_open_multipart()表单helper确保您的文件夹具有正确的权限

另请注意,请确保您已将文件和类名命名为 signup.php Signup.php 只有第一个字母在课程上应为大写且文件名在这里解释https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming

<?php

class Signup extends CI_Controller {

}

答案 1 :(得分:0)

删除验证检查所需,因为当您检查空文件字段时不允许传入图像字段(!空($ _ FILES [&#39;图片&#39;] [&#39;名称&#39; ])然后需要验证已在其中检查,其次你必须检查目录是否已创建并提供777权限。我通过仅添加这两个检查来测试代码。希望它有所帮助

 $this->form_validation->set_rules('picture', 'Image', 'trim'); /*Change in this line -- remove required*/
        if ($this->form_validation->run()) {
            if (!empty($_FILES['picture']['name'])) {
                $config['upload_path'] = 'uploads/images/';
                /*add 777 permission to directory*/  
                if (!is_dir($config['upload_path'])) {
                    mkdir($config['upload_path'], 0777, TRUE);
                }
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['max_size'] = 10000000;
                $config['file_name'] = $_FILES['picture']['name'];

                //Load upload library and initialize configuration
                $this->load->library('upload', $config);
                $this->upload->initialize($config);

                if ($this->upload->do_upload('picture')) {
                    $uploadData = $this->upload->data();
                    $picture = $uploadData['file_name'];
                } else {
                    $picture = '';
                    $error = array('error' => $this->upload->display_errors());
                    echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '" . base_url("index.php/signup") . "';</script>";
                }
            } else {
                $picture = '';
            }
            $data = array(
                'image' => $picture
            );

            $this->write_conn->db->insert('test', $data);
            if ($this->write_conn->db->insert('test', $data)) {
                $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>');
            } else {
                // error
                $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
            }
        }