데이터베이스에 이미지를 업로드하는 방법은 폴더에 저장합니까?

나윈

저는 CI를 처음 사용합니다. 누구든지 CI 2.0 용 이미지 업로드 튜토리얼을 제공 할 수 있습니까? 자산 폴더에 Product_image 폴더를 만들었습니다.테이블 이름은 tbl_product. 내 컨트롤러 페이지는 제품이고 코드는 다음과 같습니다.

<?php 
include_once('application/backend/controllers/dashboard.php');

 class Product extends Dashboard {
    function _construct(){
        parent::_construct();
        $this->load->model('product_model');
}

 public function index() {

   $this->islogin();
   $data=$this->generateCommonItems();
   $this->load->model('product_model');
   $data['page_title']="List of Products";
   $data['page_heading']="List of Products";
   $data['listAllProduct']=$this->product_model->listAllProduct();
   $this->load->view('products/listproduct',$data);
}

function addproduct() {
    $this->data['title'] = 'Add Product';

    //validate form input
    $this->form_validation->set_rules('prod_name', 'Product name',     'required|xss_clean');
    $this->form_validation->set_rules('prod_des', 'Description', 'required|xss_clean');
    $this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
    $this->form_validation->set_rules('prod_image', 'Picture', 'required|xss_clean');

    if ($this->form_validation->run() == true)
    {       
        $data = array(
            'prod_name'             => $this->input->post('prod_name'),
            'prod_des'      => $this->input->post('prod_des'),
            'price'             => $this->input->post('price'),
            'prod_image'            => $this->input->post('prod_image')
        );

        $this->product_model->insert_product($data);
        redirect('product/addproduct');

    } else {
        $data=$this->generateCommonItems();
        //display the add product form
        //set the flash data error message if there is one

        $this->data['prod_name'] = array(
            'name'      => 'name',
            'id'        => 'name',
            'type'      => 'text',
            'style'     => 'width:300px;',
            'value'     => $this->form_validation->set_value('name'),
        );          
        $this->data['prod_des'] = array(
            'name'      => 'description',
            'id'        => 'description',
            'type'      => 'text',
            'cols'      =>  60,
            'rows'      =>  5,
            'value'     => $this->form_validation->set_value('description'),
        );
        $this->data['price'] = array(
            'name'      => 'price',
            'id'        => 'price',
            'type'      => 'text',
            'style'     => 'width:40px;text-align: right',
            'value'     => $this->form_validation->set_value('price'),
        );
        $this->data['prod_image'] = array(
            'value' => $this->form_validation->set_value('prod_image'),
        );

        $this->load->view('includes/header',$data);
        $this->load->view('products/product_form', $this->data);
                    $this->load->view('includes/footer',$data);
    }
}

 public function delete($prod_id) {

   $this->islogin();
   $this->load->model('product_model');
    if($this->product_model->deleteByid($prod_id))
        redirect('product/index');
}

function edit_product($prod_id) {
    $product = $this->product_model->get_product($prod_id);

    $this->data['title'] = 'Edit Product';

    //validate form input
    $this->form_validation->set_rules('prod_name', 'Product name', 'required|xss_clean');$this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
    $this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
    $this->form_validation->set_rules('prod_image', 'Picture', 'required|xss_clean');

    if (isset($_POST) && !empty($_POST))
    {       
        $data = array(
            'prod_name'                 => $this->input->post('prod_name'),
            'prod_des'          => $this->input->post('prod_des'),
            'price'                 => $this->input->post('price'),
            'prod_image'                => $this->input->post('prod_image'),
        );

        if ($this->form_validation->run() === true)
        {
            $this->product_model->update_product($prod_id, $data);          
            redirect(base_url().'product/edit/'.$prod_id);
        }           
    }       
    $this->data['tbl_product'] = $product;

    //display the edit product form
    $this->data['prod_name'] = array(
        'name'      => 'name',
        'id'        => 'name',
        'type'      => 'text',
        'style'     => 'width:300px;',
        'value'     => $this->form_validation->set_value('name', $product['name']),
    );

    $this->data['prod_des'] = array(
        'name'      => 'description',
        'id'        => 'description',
        'type'      => 'text',
        'cols'      =>  60,
        'rows'      =>  5,
        'value'     => $this->form_validation->set_value('description', $product['description']),
    );

    $this->data['price'] = array(
        'name'      => 'price',
        'id'        => 'price',
        'type'      => 'text',
        'style'     => 'width:40px;text-align: right',
        'value'     => $this->form_validation->set_value('price', $product['price']),
    );

    $this->data['prod_image'] = array(
        'name'  => 'picture',
        'id'    => 'picture',
        'type'  => 'text',
        'style' => 'width:250px;',
        'value' => $this->form_validation->set_value('prod_image', $product['picture']),
    );

    $this->load->view('products/edit_product', $this->data);
}

}

내 모델 페이지는 product_model입니다 .

<?php class Product_model extends CI_Model {

 function __construct() {

     parent::__construct();
}

function listAllProduct() {
    $query = $this->db->select('*')->from('tbl_product')->get();
    //return $query = result();
    //$query = $this->db->query("select * from tbl_product");
    return $query->result();    
}

function get_product($prod_id) {
    $this->db->select('prod_id, prod_name, prod_des, price, prod_image');
    $this->db->where('prod_id', $prod_id);
    $query = $this->db->get('tbl_product');

    return $query->row_array();
}

public function insert_product($data) {
    if ($this->db->insert('tbl_product', $data))
        return true;
    else {
        return false;
    }
}

public function update_product($prod_id, $data) {
    $this->db->where('prod_id', $prod_id);
    $this->db->update('tbl_product', $data);
}

function deleteByid($prod_id) {
    $this->db->where('prod_id', $prod_id);
    if ($this->db->delete('tbl_product')) {
        return true;
    } else {
        return false;
    }

전망:

    <h2 align="center">Add Product</h2>
     <?php echo form_open("product/addproduct");?>
     <table width="700" border="1" cellpadding="0" cellspacing="2" align="center">
        <tr>
            <td width="130" align="right" bgcolor="#FFFFFF">Product Name: </td>
            <td><?php echo form_input($prod_name);?></td>
        </tr>
        <tr>
            <td width="130" align="right" bgcolor="#FFFFFF">Product Description: </td>
            <td><?php echo form_textarea($prod_des); ?></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FFFFFF">Price:</td>
            <td><?php echo form_input($price); ?></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FFFFFF">Picture:</td>
            <td><?php echo form_input($prod_image); ?></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
            <td><?php echo form_submit('submit', 'Submit');?>
        </tr>
    </table>
<?php echo form_close(); ?>

양식 페이지보기 :

      <h2 align="center">Add Product</h2>
      <?php echo form_open("product/addproduct");?>
      <table width="700" border="1" cellpadding="0" cellspacing="2" align="center">
        <tr>
            <td width="130" align="right" bgcolor="#FFFFFF">Product Name: </td>
            <td><?php echo form_input($prod_name);?></td>
        </tr>
        <tr>
            <td width="130" align="right" bgcolor="#FFFFFF">Product Description: </td>
            <td><?php echo form_textarea($prod_des); ?></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FFFFFF">Price:</td>
            <td><?php echo form_input($price); ?></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FFFFFF">Picture:</td>
            <td><?php echo form_input($prod_image); ?></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
            <td><?php echo form_submit('submit', 'Submit');?>
        </tr>
    </table>
<?php echo form_close(); ?>

실수 n 오류를 확인 하여이 오류 코드에 대한 해결책을 주신 그 / 그녀에게 감사드립니다.

아서 마스트로 피에트로

1 : 양식에 파일을 업로드 할 때 open_multipart 속성을 사용해야합니다.

<?php echo form_open_multipart('product/addproduct');?>

2 : 양식 유효성 검사에서 파일 양식 속성을 제거합니다. 다음을 삭제합니다.

$this->form_validation->set_rules('prod_image', 'Picture', 'required|xss_clean');

3 : 이것을 사용하여 폴더에 파일을 저장합니다.

$config['upload_path'] = './asset/Product_image/'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
$config['max_size'] = '2048'; //The max size of the image in kb's
$config['max_width']  = '1024'; //The max of the images width in px
$config['max_height']  = '768'; //The max of the images height in px
$config['overwrite'] = TRUE; //If exists an image with the same name it will overwrite. Set to false if don't want to overwrite
$this->load->library('upload', $config); //Load the upload CI library
if (!$this->upload->do_upload('userfile')){
    $uploadError = array('upload_error' => $this->upload->display_errors()); 
    $this->set_flashdata('uploadError', $uploadError, $urlYouWantToReturn); //If for some reason the    upload could not be done, returns the error in a flashdata and redirect to the page you specify in $urlYouWantToReturn
    exit;
}

4 : 그 후에 파일 이름을 가져와 데이터베이스에 파일 참조를 저장합니다. 바로 아래에 넣으십시오.

$file_info = $this->upload->data('userfile');
$file_name = $file_info['file_name']; //Now you got the file name in the $file_name var. Use it to record in db.
//You can assign it to your data array to pass to your update_product function.

5 : 이제 전체 코드가 병합되었습니다.

function addproduct() {
$this->data['title'] = 'Add Product';

//validate form input
$this->form_validation->set_rules('prod_name', 'Product name',     'required|xss_clean');
$this->form_validation->set_rules('prod_des', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required|xss_clean');

if ($this->form_validation->run() == true)
{
    $config['upload_path'] = './asset/Product_image/'; //The path where the image will be save
    $config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
    $config['max_size']    = '2048'; //The max size of the image in kb's
    $config['max_width']  = '1024'; //The max of the images width in px
    $config['max_height']  = '768'; //The max of the images height in px
    $config['overwrite'] = TRUE; //If exists an image with the same name it will overwrite. Set to  false if don't want to overwrite
    $this->load->library('upload', $config); //Load the upload CI library
    if (!$this->upload->do_upload('userfile')){
        $uploadError = array('upload_error' => $this->upload->display_errors()); 
        $this->set_flashdata('uploadError', $uploadError, $urlYouWantToReturn); //If for some reason the upload could not be done, returns the error in a flashdata and redirect to the page you specify in $urlYouWantToReturn
       exit;
    }
    $file_info = $this->upload->data('userfile');
    $file_name = $file_info['file_name']; //Now you got the file name in the $file_name var. Use it to record in db.
    $data = array(
        'prod_name' => $this->input->post('prod_name'),
        'prod_des'  => $this->input->post('prod_des'),
        'price'     => $this->input->post('price'),
        'prod_image'=> $file_name,
    );

    $this->product_model->insert_product($data);
    redirect('product/addproduct');

} else {
    $data=$this->generateCommonItems();
    //display the add product form
    //set the flash data error message if there is one

    $this->data['prod_name'] = array(
        'name'      => 'name',
        'id'        => 'name',
        'type'      => 'text',
        'style'     => 'width:300px;',
        'value'     => $this->form_validation->set_value('name'),
    );          
    $this->data['prod_des'] = array(
        'name'      => 'description',
        'id'        => 'description',
        'type'      => 'text',
        'cols'      =>  60,
        'rows'      =>  5,
        'value'     => $this->form_validation->set_value('description'),
    );
    $this->data['price'] = array(
        'name'      => 'price',
        'id'        => 'price',
        'type'      => 'text',
        'style'     => 'width:40px;text-align: right',
        'value'     => $this->form_validation->set_value('price'),
    );
    $this->data['prod_image'] = array(
        'value' => $this->form_validation->set_value('prod_image'),
    );

    $this->load->view('includes/header',$data);
    $this->load->view('products/product_form', $this->data);
    $this->load->view('includes/footer',$data);
}

}

이것이 제가 CI 프로젝트에서 사용하는 것입니다. 도움이되기를 바랍니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

DreamFactory : 파일 서버에 이미지를 업로드하고 데이터베이스에 이미지 경로를 저장하는 방법은 무엇입니까?

분류에서Dev

Laravel에서 이미지 경로를 데이터베이스에 저장하는 방법은 무엇입니까?

분류에서Dev

ASP.NET Core를 사용하여 데이터베이스의 다른 폴더에 이미지를 저장하는 방법은 무엇입니까?

분류에서Dev

이미지를 업로드하고 react를 사용하여 데이터베이스에 저장하는 방법

분류에서Dev

이미지를 업로드하고 Codeigniter의 데이터베이스에 저장하는 방법은 무엇입니까?

분류에서Dev

드로어 블 폴더에 이미지가있는 데이터베이스에 이미지 경로 / 이미지를 저장하는 방법은 무엇입니까?

분류에서Dev

이미지를 데이터베이스에 저장하는 방법은 무엇입니까?

분류에서Dev

자산 폴더에서 sqlite 데이터베이스를 업데이트하는 방법은 무엇입니까?

분류에서Dev

폴더에 이미지를 저장하는 방법은 무엇입니까?

분류에서Dev

데이터베이스에 이미지 경로를 저장하는 방법

분류에서Dev

이미지를 저장하고 비트 맵을 사용하여 sqlite 데이터베이스 또는 드로어 블 폴더에서 검색하는 방법

분류에서Dev

업로드 된 이미지 이름을 MySQL 데이터베이스에 저장하는 방법

분류에서Dev

바이너리에 저장된 데이터베이스의 이미지를 div 배경 이미지로 정의하는 방법은 무엇입니까?

분류에서Dev

데이터베이스에서 HTML로 이미지를로드하는 방법은 무엇입니까?

분류에서Dev

zip 폴더의 csv 파일에서 읽고 데이터베이스의 csv 파일에서 데이터를 저장하는 방법은 무엇입니까?

분류에서Dev

데이터베이스의 데이터를 mvc 응용 프로그램의 폴더에 저장하는 방법

분류에서Dev

이름을 사용하여 데이터베이스의 폴더에서 원하는 이미지를 하나만 검색하는 방법은 무엇입니까?

분류에서Dev

phpmyadmin 데이터베이스에서 pdf로 데이터를 저장하는 방법은 무엇입니까?

분류에서Dev

Java를 사용하여 긴 blob (이미지)을 mysql 데이터베이스에 업로드하고 PHP에서 검색하는 방법은 무엇입니까?

분류에서Dev

데이터베이스에 저장된 항목에서만 합계를 얻는 방법은 무엇입니까?

분류에서Dev

소스가 SQL Server 데이터베이스에 저장된 HTML 테이블로 이미지를 검색하는 방법은 무엇입니까?

분류에서Dev

크로스 플랫폼 애플리케이션 용 Xamarin Forms의 sqlite 데이터베이스에 이미지를 저장하는 방법은 무엇입니까?

분류에서Dev

Android에서 프로필 사진을 변경할 때 이미지를 저장 폴더와 데이터베이스에 저장하는 방법은 무엇입니까?

분류에서Dev

HTML 페이지에서 PNG 이미지를 만들고 Django를 사용하여 데이터베이스에 저장하는 방법은 무엇입니까?

분류에서Dev

데이터베이스에 업로드하기 전에 이미지를 끌어서 재배치하는 방법은 무엇입니까?

분류에서Dev

reactjs와 PHP를 사용하여 이미지를 데이터베이스에 업로드하는 방법은 무엇입니까?

분류에서Dev

이미지를 데이터베이스에 저장하고 검색하는 가장 좋은 방법

분류에서Dev

이진 데이터로 데이터베이스에 저장된 이미지의 확장자를 찾을 수있는 방법이 있습니까?

분류에서Dev

laravel5.4의 저장 폴더에 저장된 이미지를 검색하는 방법은 무엇입니까?

Related 관련 기사

  1. 1

    DreamFactory : 파일 서버에 이미지를 업로드하고 데이터베이스에 이미지 경로를 저장하는 방법은 무엇입니까?

  2. 2

    Laravel에서 이미지 경로를 데이터베이스에 저장하는 방법은 무엇입니까?

  3. 3

    ASP.NET Core를 사용하여 데이터베이스의 다른 폴더에 이미지를 저장하는 방법은 무엇입니까?

  4. 4

    이미지를 업로드하고 react를 사용하여 데이터베이스에 저장하는 방법

  5. 5

    이미지를 업로드하고 Codeigniter의 데이터베이스에 저장하는 방법은 무엇입니까?

  6. 6

    드로어 블 폴더에 이미지가있는 데이터베이스에 이미지 경로 / 이미지를 저장하는 방법은 무엇입니까?

  7. 7

    이미지를 데이터베이스에 저장하는 방법은 무엇입니까?

  8. 8

    자산 폴더에서 sqlite 데이터베이스를 업데이트하는 방법은 무엇입니까?

  9. 9

    폴더에 이미지를 저장하는 방법은 무엇입니까?

  10. 10

    데이터베이스에 이미지 경로를 저장하는 방법

  11. 11

    이미지를 저장하고 비트 맵을 사용하여 sqlite 데이터베이스 또는 드로어 블 폴더에서 검색하는 방법

  12. 12

    업로드 된 이미지 이름을 MySQL 데이터베이스에 저장하는 방법

  13. 13

    바이너리에 저장된 데이터베이스의 이미지를 div 배경 이미지로 정의하는 방법은 무엇입니까?

  14. 14

    데이터베이스에서 HTML로 이미지를로드하는 방법은 무엇입니까?

  15. 15

    zip 폴더의 csv 파일에서 읽고 데이터베이스의 csv 파일에서 데이터를 저장하는 방법은 무엇입니까?

  16. 16

    데이터베이스의 데이터를 mvc 응용 프로그램의 폴더에 저장하는 방법

  17. 17

    이름을 사용하여 데이터베이스의 폴더에서 원하는 이미지를 하나만 검색하는 방법은 무엇입니까?

  18. 18

    phpmyadmin 데이터베이스에서 pdf로 데이터를 저장하는 방법은 무엇입니까?

  19. 19

    Java를 사용하여 긴 blob (이미지)을 mysql 데이터베이스에 업로드하고 PHP에서 검색하는 방법은 무엇입니까?

  20. 20

    데이터베이스에 저장된 항목에서만 합계를 얻는 방법은 무엇입니까?

  21. 21

    소스가 SQL Server 데이터베이스에 저장된 HTML 테이블로 이미지를 검색하는 방법은 무엇입니까?

  22. 22

    크로스 플랫폼 애플리케이션 용 Xamarin Forms의 sqlite 데이터베이스에 이미지를 저장하는 방법은 무엇입니까?

  23. 23

    Android에서 프로필 사진을 변경할 때 이미지를 저장 폴더와 데이터베이스에 저장하는 방법은 무엇입니까?

  24. 24

    HTML 페이지에서 PNG 이미지를 만들고 Django를 사용하여 데이터베이스에 저장하는 방법은 무엇입니까?

  25. 25

    데이터베이스에 업로드하기 전에 이미지를 끌어서 재배치하는 방법은 무엇입니까?

  26. 26

    reactjs와 PHP를 사용하여 이미지를 데이터베이스에 업로드하는 방법은 무엇입니까?

  27. 27

    이미지를 데이터베이스에 저장하고 검색하는 가장 좋은 방법

  28. 28

    이진 데이터로 데이터베이스에 저장된 이미지의 확장자를 찾을 수있는 방법이 있습니까?

  29. 29

    laravel5.4의 저장 폴더에 저장된 이미지를 검색하는 방법은 무엇입니까?

뜨겁다태그

보관