Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用codeigniter上传图片?_Php_Codeigniter - Fatal编程技术网

Php 如何使用codeigniter上传图片?

Php 如何使用codeigniter上传图片?,php,codeigniter,Php,Codeigniter,我可以设法将数据插入tblaccount。但问题是图片无法上传。tblaccount包含firstname、lastname、email、department、username和password的正确数据,但是即使我上传了一些图片,图片仍然是空白的 更新:图片现在可以上传到文件夹中,但tblaccount中没有运气。它只显示空白数据 signup.php控制器: public function index() { // set form validation rules $thi

我可以设法将数据插入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');
            }
    }
}
公共功能索引()
{
//设置表单验证规则
$this->form_validation->set_rules('firstname','First Name','trim | required | alpha | minu_length[3]| max_length[30]);
$this->form_validation->set_rules('lastname','lastname','trim | required | alpha | minu_length[3]| max_length[30]);
$this->form_validation->set_rules('email','email Address','trim | required | valid|email |是唯一的[tblaccount.email]');
$this->form_validation->set_rules('department'、'department'、'trim | required | alpha | min|u length[3]| max|u length[30]);
$this->form_validation->set_rules('username'、'username'、'trim | required | alpha | minu_length[3]| max_length[30]|是唯一的[tblaccount.username]);
$this->form_validation->set_规则('password'、'password'、'trim | required');
$this->form_validation->set_规则('cpassword','Confirm Password','trim | required |匹配[密码]);
$this->form_validation->set_规则('picture'、'Image'、'trim | required');
//提交
如果($this->form\u validation->run()==FALSE)
{
//失败
$this->load->view('signup_view');
}
其他的
{
如果(!空($_文件['picture']['name']))
{
$config['upload_path']='./uploads/images/';
$config['allowed_types']='jpg | jpeg | png | gif';
$config['max_size']=10000000;
$config['file_name']=$_FILES['picture']['name'];
//加载上载库并初始化配置
$this->load->library('upload',$config);
$this->upload->initialize($config);
如果($this->upload->do_upload('picture'))
{
$uploadData=$this->upload->data();
$picture=$uploadData['file_name'];
}
其他的
{
$picture='';
$error=array('error'=>$this->upload->display_errors());
echo“警报('只允许JPG、JPEG、PNG和GIF类型的文件,大小至少为10MB');window.location='”。base_url(“index.php/signup”)。“;”;
}
}
其他的
{
$picture='';
}
$data=数组(
'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
);
如果($this->account\u model->insert($data))
{
$this->session->set_flashdata('msg','您已成功注册!请登录以访问您的个人资料!');
重定向(“登录”);
}
其他的
{
//错误
$this->session->set_flashdata('msg','Oops!错误。请稍后再试!!!');
重定向(“注册”);
}
}
}
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>

报名
名字
在您的上传路径上

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

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

另一个注意事项是,确保文件和类名的命名正确,其中signup.php将是signup.php这里解释的类和文件名中只有第一个字母应该是大写


当您检查空文件字段(!empty($\u FILES['picture']['name'])时,请从验证检查中删除required,因为它不允许传入图像字段然后所需的验证已经在它里面检查过了,其次你必须检查目录是否被创建,并为它提供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>');
            }
        }
$this->form_validation->set_rules('picture'、'Image'、'trim');/*此行的更改--需要删除*/
如果($this->form\u validation->run()){
如果(!空($_文件['picture']['name'])){
$config['upload_path']=“uploads/images/”;
/*将777权限添加到目录*/
如果(!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'];
//加载上载库并初始化配置
$this->load->library('upload',$config);
$this->upload->initialize($config);
如果($this->upload->do_upload('picture')){
$uploadData=$this->upload->data();
$picture=$uploadData['file_name'];
}否则{
$picture='';
$error=array('error'=>$this->upload->display_errors());
echo“警报('只允许JPG、JPEG、PNG和GIF类型的文件,大小至少为10MB');window.location='”。base_url(“index.php/signup”)。“;”;
}
}否则{
$picture='';
}
$data=数组(
“图像”=>$picture
);
$this->write_conn->db->insert('test',$data);
如果($this->write\u conn->db->insert('test',$data)){
$this->sessi
<?php

class Signup extends CI_Controller {

}
 $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>');
            }
        }