数据未在会话代码点火器中设置


data not setting in session codeigniter

//uploading product movie or image?
        if($this->input->post('upload_360') == "Upload") {
            $config['upload_path'] = './media/images/products/360s';
            $config['allowed_types'] = 'swf';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('film')) {
                $this->data['product_error'] = $this->upload->display_errors();
                $this->template->build('/admin/products/create', $this->data);
            } else {
                $this->data['data_360'] = $this->upload->data();
                $this->session->set_userdata(array('360_film' => $this->data['data_360']));
                $this->template->build('/admin/products/create', $this->data);
            }
            $this->session->set_userdata(array('advantages' => $this->input->post('product_advantages')));
            $this->data['session_advantages'] = $this->session->userdata('advantages');
        }
        //upload the product image, if successful the user will be
        //notified if the image is too high or wide, and will be offered,
        //the chance to crop the image. All cropping takes place in the media
        //controller.
        if($this->input->post('product_image') == "Upload") {
            $config['upload_path'] = './media/images/products/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('image_upload')) {
                //die("!");
                $this->data['image_error'] = $this->upload->display_errors();
                $this->template->build('/admin/products/create', $this->data);
            } else {
                $this->data['image_data'] = $this->upload->data();
                $this->session->set_userdata(array('image' => $this->data['image_data']));
                $this->data['session_image'] = $this->session->userdata('image');
                $this->template->build('/admin/products/create', $this->data);
            }
            $this->session->set_userdata(array('advantages' => $this->input->post('product_advantages')));
            $this->data['session_advantages'] = $this->session->userdata('advantages');
        }
        if($this->input->post('screenshot_upload') == "Upload") {
            $config['upload_path'] = './media/images/products/360s/screenshots/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('screenshot')) {
                //die("!");
                $this->data['screenshot_error'] = $this->upload->display_errors();
                $this->template->build('/admin/products/create', $this->data);
            } else {
                $this->data['screenshot_data'] = $this->upload->data();
                $this->session->set_userdata(array('screenshot' => $this->data['screenshot_data']));
                $this->data['session_screenshot'] = $this->session->userdata('screenshot');
                $this->template->build('/admin/products/create', $this->data);
            }
            $this->session->set_userdata(array('advantages' => $this->input->post('product_advantages')));
            $this->data['session_advantages'] = $this->session->userdata('advantages');
        }

在我的表单上,我让用户选择一个文件,然后单击上传按钮取决于单击哪个按钮,上传文件并将上传数据保存在会话中。

然后会话用于从中获取数据以保存到数据库,upload_360会话有效,product_image会话工作正常,但screenshot_upload会话仅在使用 if 语句(代码中的第 3 条(时才有数据,如果我尝试在代码之外访问它,那么会话的那部分是空的?

这有什么原因吗?

为什么要在将数据插入数据库之前将数据存储在会话中?

Cookie 只能保存 4KB 的数据...

但是screenshot_upload会话只有在使用 if 语句(代码中的第三个(时才有数据,如果我尝试在代码之外访问它,那么会话的那部分是空的?

我不明白你问题的那部分。您的意思是它仅在使用第 3 个if语句时才有数据吗?即当只试图做screenshot_upload而不是product_image或360_upload"时?如果是这样,这可能说明 cookie 大小限制。

而不是

$this->session->set_userdata(array('screenshot' => $this->data['screenshot_data']));
$this->data['session_screenshot'] = $this->session->userdata('screenshot');

你为什么不

$this->uploads_model->insert_screenshot_data($this->data['screenshot_data']);//send screenshot upload_data to model to be inserted into db
$this->data['screenshot_data'] = $this->data['screenshot_data'];//if you want to pass screenshot upload_data to template/view

看起来您在设置会话之前将输出发送给用户(我从自定义代码$this->template->build推断这一点。

会话与标头一样,在将任何内容(ANYTHING(发送到输出后无法修改。这是因为会话本身是在标头中发送的。