简体   繁体   English

如何使用codeigniter存储图像并从文件夹中检索?

[英]How to store image and retrieve from the folder using codeigniter?

I am new to the CodeIgniter programming.I want to store and retrieve image from the folder.But when i run the code i found the error like: 我是CodeIgniter编程的新手,我想存储和检索文件夹中的图像,但是当我运行代码时,发现了如下错误:

First Error: 第一个错误:

Upload failed!

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: in

Filename: controllers/main.php

Line Number: 104

Second Error: 第二个错误:

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: application/models/main_model.php

Line Number: 80

I am using this code: 我正在使用此代码:

In Control: 掌控之中:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller{
public function __construct()
{
    parent::__construct();
    $this->load->model('main_model');
     $this->load->helper(array('form', 'url'));
}
  public function product()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('productname','Product Code','trim|required');
    $this->form_validation->set_rules('productcode','Product Code','trim|required');
    $this->form_validation->set_rules('productprice','Product Price','trim|required');
    $this->form_validation->set_rules('quantity','Quantity','trim|required');
    $this->form_validation->set_rules('uploadimage','Upload Image','trim|required');
    if($this->form_validation->run()==FALSE)
    {
        $this->index();
    }else
        {
            if ($this->input->post('upload'))
            {
                $in=array();

$in['productname']    = $this->input->post('productnamename');
$in['productcode'] = $this->input->post('productcode');
$in['productprice']=$this->input->post('productprice');
$in['quantity']=$this->input->post('quantity');
$in['uploadimage']=$_FILES['image']['name'];
            }
            if($this->main_model->do_upload()) {

echo $this->upload->display_errors();

}else
    {
        $this->main_model->save_gallery($in);
        header('location:product');
    }
            $data['images']=$this->main_model->get_images();
              $this->load->view('query_view',$data);
        }
}

In Model: 在模型中:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 class Main_model extends CI_Model {
 public function __construct()
{
   parent::__construct();
 }
    public function do_upload()
{
         $config = array(
        'allowed_types' => 'jpg|png|bmp', 
        'upload_path'=>'./uploads/', //make sure you have this folder
        'max_size'=>2000);
         $this->load->library('upload',$config);

    if ($this->upload->do_upload()) {
        echo "Upload success!";
    } else {
        echo "Upload failed!";
    }
 $image_data = $this->upload->data();
}  
 function get_images()
{
    $query = $this->db->get('product');
    return $query;
}

function save_gallery($in)
{
$save=$this->db->get("product");
if($save->num_rows())
{
$save=$this->db->insert('product',$in);
return $save;
}
}

In View: 在视图中:

 <?php foreach ($images as $image):?>
<h1><?php echo $image['a_name'];?></h1>
<h1><?php echo $image['a_details'];?></h1>
<?php echo '<img src ="'. base_url().'images1/'.$image['a_photo'].'" >";
 endforeach; ?>

FronPage View: FronPage视图:

<?php echo form_open("main/product"); ?>
    <p>
        <label for="product_name">Product Name:</label>
        <input type="text" id="productname" name="productname"       value="<?php echo set_value('product_name'); ?>" />
    </p>        


    <p>
        <label for="ProductCode">Product Code</label>
        <input type="text" id="productcode" name="productcode" value="<?php echo set_value('productcode'); ?>" />
    </p>
    <p>
        <label for="productprice">Product Price:</label>
        <input type="text" id="productprice" name="productprice" value="<?php echo set_value('productprice'); ?>" />
    </p>
    <p>
        <label for="Quantity">Quantity:</label>
        <select name="quantity" id="quantity" value="<?php echo set_value('quantity'); ?>" /><option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
        </select>

    </p>  
    <p>
        <label for="Uploadimage">Upload Image:</label>
        <input type="file" name="uploadimage" id="uploadimage" value="<?php echo set_value('uploadimage'); ?>" />
    </p>

    <p>
        <input type="submit" class="greenButton" value="submit" />
    </p>
<?php echo form_close(); ?>

First and foremost, read the documentation at http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html . 首先,请阅读http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html上的文档。 Follow it verbatim, nothing fancy, no database, no extra conditionals. 逐字遵循,没有任何幻想,没有数据库,没有额外的条件。 Get that working and add new functionality incrementally in very small portions. 使它正常工作,并在很小的一部分中逐步添加新功能。 As you encounter problems look around for the answer. 当您遇到问题时,请寻找答案。

Now for the problem.. 现在解决这个问题。

You are not getting a value for $this->input->post('upload') which is only one of many issues. 您没有获得$this->input->post('upload') ,这只是许多问题之一。

Your form open should be 您打开的表格应为

<?php echo form_open_multipart("main/product"); ?>

The error below generally indicates that you are passing an empty variable to the insert function 以下错误通常表示您正在将一个空变量传递给insert函数

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: application/models/main_model.php

Line Number: 80

Why are you passing an empty variable? 为什么要传递一个空变量? You are calling the do_upload method which always returns void, so, the TRUE condition is never met in the code below. 您正在调用do_upload方法,该方法始终返回void,因此,下面的代码永远不会满足TRUE条件。

if($this->main_model->do_upload()) {
  echo $this->upload->display_errors();
}
else
{
  $this->main_model->save_gallery($in);
  header('location:product');
}

Verify you are passing the input named upload in the form, and the condition for upload should be more like this 验证您是否在表单中传递名为upload的输入,并且上载的条件应更像这样

    if(isset($_FILES['upload']['name']))
    {
        $in=array();

        $in['productname']    = $this->input->post('productnamename');
        $in['productcode'] = $this->input->post('productcode');
        $in['productprice']=$this->input->post('productprice');
        $in['quantity']=$this->input->post('quantity');
        $in['uploadimage']=$_FILES['image']['name'];

        // moved to inside the post('upload') conditional
        if($this->main_model->do_upload()) {
            echo $this->upload->display_errors();
        }
        else
        {
            $this->main_model->save_gallery($in);
            header('location:product');
        }

    }

1) Your form enctype must be multipart 1)您的表单enctype必须是multipart

2) your are checking post instead of $_FILES 2)您正在检查post而不是$_FILES

it should be if ($this->input->post('upload')) to if ($_FILES['upload']['name']) for this reason it is not coming in your if block therefore $in is undefined when you call 由于这个原因,它应该不是if ($this->input->post('upload'))if ($_FILES['upload']['name'])的原因,因此它不在您的if块中,因此$in是通话时不确定

$this->main_model->save_gallery($in);

when $in is undefined therefore you see erro You must use the "set" method to update an entry. $in未定义时$in您将看到错误。 You must use the "set" method to update an entry. in the model 在模型中

Hope it makes sense 希望有道理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM