会话 Codeigniter 中未设置数据

发布于 2025-01-05 19:32:31 字数 4033 浏览 1 评论 0原文

//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 语句(代码中的第三个)时才有数据(如果我尝试访问它)在代码之外,那么会话的那部分是空的?

这是有原因的吗?

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

On my form I have the user choose a file and the click an upload button dependent on which button is clicked the file gets uploaded and the upload data gets saved in a session.

The session is then used to get data from to save to a database, the upload_360 session works, the product_image session works fine but the screenshot_upload session only has data when with the if statement (3rd one in the code) if I try and acccess it outside of the code then that portion of the session is empty?

Is there a reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2025-01-12 19:32:31

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

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

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

我不明白你问题的那部分。您的意思是仅在使用第三个 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

Why are you storing data in a session before inserting it into the db?

Cookies can only hold 4KB of data...

but the screenshot_upload session only has data when with the if statement (3rd one in the code) if I try and acccess it outside of the code then that portion of the session is empty?

I don't understand that part of your question. Do you mean it only has data when only using the 3rd if statement? i.e. when only trying to do screenshot_upload and not product_image or 360_upload`? If so, that might speak to the cookie size limit.

Instead of

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

why don't you

$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

?

漫漫岁月 2025-01-12 19:32:31

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

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

It looks like you are sending output to the user before setting the session (I'm inferring this from the $this->template->build, which is custom code.)

The session, like headers, cannot be modified after anything (ANYTHING) has been sent to output. This is because the session itself is sent in the header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文