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

标签 php html mysql codeigniter

我是 CodeIgniter 编程的新手。我想从文件夹中存储和检索图像。但是当我运行代码时,我发现错误如下:

第一个错误:

Upload failed!

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: in

Filename: controllers/main.php

Line Number: 104

第二个错误:

A Database Error Occurred

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

Filename: application/models/main_model.php

Line Number: 80

我正在使用这段代码:

在控制中:

<?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);
        }
}

在模型中:

 <?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;
}
}

在 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; ?>

首页 View :

<?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(); ?>

最佳答案

首先,阅读 http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html 上的文档.逐字逐句,没有什么花哨的,没有数据库,没有额外的条件。让它工作并以非常小的部分增量添加新功能。当你遇到问题时,四处寻找答案。

现在问题来了..

您没有获得 $this->input->post('upload') 的值这只是众多问题中的一个。

你的表单打开应该是

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

下面的错误通常表示您正在将一个空变量传递给 insert功能

A Database Error Occurred

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

Filename: application/models/main_model.php

Line Number: 80

为什么要传递一个空变量?您正在调用 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');
}

验证您正在传递名为 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');
        }

    }

关于php - 如何使用 codeigniter 存储图像并从文件夹中检索图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17320640/

相关文章:

php - Mysqli 期望行数作为参数

php - Woocommerce REST API - 如果实时网站上的订单与其他网站上的产品不匹配,则跳过订单传输

html - CSS 问题::之前在手机上

javascript - HTML 导入在 Electron 应用程序中不起作用

css - 在所有屏幕尺寸上居中网站

php - CodeIgniter 事件记录 : Debugging without running SQL

php - 使用 Imagick 将每个 PDF 页面保存为图像

mysql - 获取查询的行数

javascript - Node 中的模块导出使用错误

java - Play Framework 使用不受支持的 MySQL 语法演变