1

I have the following form and controller where it has a image upload, but everything goes smooth except the file not being uploaded to the particular folder.

View

<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce cms - home page</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />

</head>

<body>

<form action="<?php echo base_url();?>index.php/addproduct_controller" method="post">
<?php
include 'header-adminpanel.php';
?>
<div class="container">

    <div class="body-content">
        <div class="side-left"><?php
include 'adminproduct_sidebar.php';
?></div>
        <div class="side-right">
        <br />
        <table>
            <tr>
                <td class="captions">Product Code</td>
                <td><input name="txt_pcode" type="text"/></td>
            </tr>
            <tr>
                <td class="captions">Product Name</td>
                <td><input name="txt_pname" type="text" size="40" /></td>
            </tr>
            <tr>
                <td class="captions">Product Price</td>
                <td><input name="txt_pprice" type="text" /></td>
            </tr>
            <tr>
                <td class="captions">Product Description</td>
                <td><textarea name="txt_pdesc" style="width:300px;height:100px;"></textarea></td>
            </tr>
            <tr>
                <td class="captions">Product Image</td>
                <td><input type="file" name="userfile" size="20" /></td>
            </tr>
            <tr>
                <td class="captions">Product Options</td>
                <td><input name="txt_poptions" size="40" type="text" /><a class="hint"> (Separate by a "," comma)</a></td>
            </tr>
            <tr><td><input name="btn_add" class="button" type="submit" value="Add" /></td></tr>
        </table>
        <br />
        </div>  
    </div> 

</div>
<div style="clear:both"></div>
<?php
include 'footer.php';
?>
</form>
</body>
</html>

Controller

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Addproduct_controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
        if (isset($_POST["btn_logout"])) {

            $this->session->sess_destroy();
            $this->load->view('welcome_view');
        } else if (isset($_POST["btn_home"])) {

            $this->load->view('welcome_view');

        } else if (isset($_POST["btn_account"])) {


        } else if (isset($_POST["btn_add"])) {

            $prod_img                = 'no image';
            $config['upload_path']   = 'Public/uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']      = '1024';
            $config['max_width']     = '1024';
            $config['max_height']    = '768';
            $config['encrypt_name']  = TRUE;



            $this->load->library('upload', $config);


            if (!$this->upload->do_upload()) {

                //  $error = array('error' => $this->upload->display_errors());

                //$this->load->view('upload_form', $error);
                //return 'error';

            } else {
                global $prod_img;
                $data     = array(
                    'upload_data' => $this->upload->data()
                );
                $prod_img = $data->file_name;
                //  $this->load->view('upload_success', $data);
            }


            $prod_name    = $_POST["txt_pname"];
            $prod_code    = $_POST["txt_pcode"];
            $prod_price   = $_POST["txt_pprice"];
            $prod_desc    = $_POST["txt_pdesc"];
            $prod_options = $_POST["txt_poptions"];



            $this->load->model('product_model');
            $addproduct_result = $this->product_model->addProduct($prod_code, $prod_name, $prod_price, $prod_desc, $prod_img);

            if ($addproduct_result == true) {

                echo "Added Successfully";

            } else {

                echo "Failed";
            }

        }

    }





}

Then I tried by adding following instead of normal tags.

<?php
$this->load->helper('form');
?>


<?php
echo form_open_multipart(base_url().'index.php/addproduct_controller');
?>

where it gaves me an error

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/addproduct_controller.php

Line Number: 53

Please help me with this or show me where I have done the mistake.

3 Answers 3

1

enctype attribute missing in your from tag.

Add enctype="multipart/form-data" in your form tag

OR

In CI, Use form_open_multipart function to generate form tag

As per discussion in comment, update your code as below.

$data     = array(
                    'upload_data' => $this->upload->data()
                );
$prod_img = $data["upload_data"]->file_name;
Sign up to request clarification or add additional context in comments.

5 Comments

it gives me an error " Trying to get property of non-object" when I added it like <?php $this->load->helper('form'); ?> <?php echo form_open_multipart(base_url().'index.php/addproduct_controller'); ?>
Yes It says "Trying to get property of non-object"
A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: controllers/addproduct_controller.php Line Number: 53
Yes, Now the file is getting uploaded, but still the error comes
I got it corrected kumar_v, issue was with the line $prod_img = $data->file_name; Thanks alot for the help :)
0

You have missed out to include form attribute to upload the file

Add enctype="multipart/form-data" in your form tag

Comments

0

You have make the html form but not added file upload tag in your form creation.

add: enctype="multipart/form-data" in your form tag.

<form action="<?php echo base_url();?>index.php/addproduct_controller" method="post" enctype="multipart/form-data" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.