在方法中设置 $this->data 然后取消设置

发布于 2025-01-02 14:56:50 字数 1439 浏览 0 评论 0原文

我正在使用 codeigniter 上传图像,在方法中我正在执行以下操作,

if(isset($_FILES['product_image']['name'])) {
            //some setup needed
            $config['upload_path'] = "./media/images/products";
            $config['allowed_types'] = "png|gif|jpg|jpeg";
            $config['max_width'] = 1490;
            $config['max_height'] = 400;
            //make sure the library is running with clean config
            $this->upload->initialize($config);
            //do the upload
            if(!$this->upload->do_upload('product_image')){
                $this->data['image_error'] = $this->upload->display_errors();
                $this->template->build('/admin/products/create', $this->data);
            } else {
                $this->data['image_data'] = $this->upload->data();
                //die(print_r($this->data));
                $this->template->build('/admin/products/create', $this->data);
            }
        }

所以基本上我检查 $_FILES 中是否有内容,然后上传是否分配 $this-data['image_data' ] 一路上传数据。但是,当我处理数据时,即将文件名保存在数据库中,我无法访问 $this->data['image_data'] 下面是我尝试使用它的方式,

if($this->input->post("submit_create") == "Save") {
    die(print_r($this->data['image_data']));
}

但是我得到出现以下错误,

消息:未定义索引:image_data

为什么我将事物分配给 $this 使它们不仅可以在整个方法中访问,而且可以在整个控制器中访问?

I am uploading an image with codeigniter, and in the method I am doing the following,

if(isset($_FILES['product_image']['name'])) {
            //some setup needed
            $config['upload_path'] = "./media/images/products";
            $config['allowed_types'] = "png|gif|jpg|jpeg";
            $config['max_width'] = 1490;
            $config['max_height'] = 400;
            //make sure the library is running with clean config
            $this->upload->initialize($config);
            //do the upload
            if(!$this->upload->do_upload('product_image')){
                $this->data['image_error'] = $this->upload->display_errors();
                $this->template->build('/admin/products/create', $this->data);
            } else {
                $this->data['image_data'] = $this->upload->data();
                //die(print_r($this->data));
                $this->template->build('/admin/products/create', $this->data);
            }
        }

so basically I check to see if there is something in the $_FILES and then upload if there is assigning $this-data['image_data'] with the upload data along the way. However when I come to process the data i.e. save the filename in a database, I cannot access $this->data['image_data'] below is how I am trying to use it,

if($this->input->post("submit_create") == "Save") {
    die(print_r($this->data['image_data']));
}

however I get the following error,

Message: Undefined index: image_data

why is this I though assigning things to $this made them accessible not just throughout the method but the entire controller?

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

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

发布评论

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

评论(1

╰◇生如夏花灿烂 2025-01-09 14:56:50

我认为,当您在示例中此时打印 image_data 时:

if($this->input->post("submit_create") == "Save") {
    die(print_r($this->data['image_data']));
}

您实际上应该使用 $image_data 来代替:

if($this->input->post("submit_create") == "Save") {
    die(print_r($image_data));
}

I think, when you are printing image_data at this point in your example:

if($this->input->post("submit_create") == "Save") {
    die(print_r($this->data['image_data']));
}

You should actually be using $image_data instead:

if($this->input->post("submit_create") == "Save") {
    die(print_r($image_data));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文