Welcome Guest, Not a member yet? Register   Sign In
upload image
#1

Hello,

I trying to practice uploading image:

http://www.codersmount.com/2012/11/uploa...deigniter/

I don't think it works:

views/main.php

PHP Code:
<?php
  $image 
= array(
 
   'name'  =>  'userfile',
 
   'id'    =>  'userfile',
 
   );
 
    
  $submit 
= array(
 
   'name'  => 'submit',
 
   'id'    => 'submit',
 
   'value' => 'Upload'
 
   );
?>
<?php 
echo form_open_multipart('upload/upload_image''id=upload_file'); ?>
<?php 
echo form_upload($image); ?>
<?php 
echo form_submit($submit); ?>
<?php 
echo form_close(); ?>


controllers/upload.php

PHP Code:
class Upload extends CI_Controller{
    
    public function 
index()
    {
        
$this->load->view('main');
    }    
    
 
   public function upload_image(){
 
       $this->upload_model->do_upload(); //execute the upload function
        
        
 
   }



models/upload_model.php

PHP Code:
<?php
class User_model extends CI_Model{
 
 var $original_path;
 
 var $resized_path;
 
 var $thumbs_path;
 
 
 //initialize the path where you want to save your images
 
 function __construct(){
 
   parent::__construct();
 
   //return the full path of the directory
 
   //make sure these directories have read and write permessions
 
   $this->original_path realpath(APPPATH.'../uploads/original');
 
   $this->resized_path realpath(APPPATH.'../uploads/resized');
 
   $this->thumbs_path realpath(APPPATH.'../uploads/thumbs');
 
 }
 
 
 function do_upload(){
 
   $this->load->library('image_lib');
 
   $config = array(
 
   'allowed_types'     => 'jpg|jpeg|gif|png'//only accept these file types
 
   'max_size'          => 2048//2MB max
 
   'upload_path'       => $this->original_path //upload directory
 
 );
 
 
   $this->load->library('upload'$config);
 
   $image_data $this->upload->data(); //upload the image
 
 
   //your desired config for the resize() function
 
   $config = array(
 
   'source_image'      => $image_data['full_path'], //path to the uploaded image
 
   'new_image'         => $this->resized_path//path to
 
   'maintain_ratio'    => true,
 
   'width'             => 128,
 
   'height'            => 128
    
);
 
 
   //this is the magic line that enables you generate multiple thumbnails
 
   //you have to call the initialize() function each time you call the resize()
 
   //otherwise it will not work and only generate one thumbnail
 
   $this->image_lib->initialize($config);
 
   $this->image_lib->resize();
 
 
   $config = array(
 
   'source_image'      => $image_data['full_path'],
 
   'new_image'         => $this->thumbs_path,
 
   'maintain_ratio'    => true,
 
   'width'             => 36,
 
   'height'            => 36
    
);
 
   //here is the second thumbnail, notice the call for the initialize() function again
 
   $this->image_lib->initialize($config);
 
   $this->image_lib->resize();
 
 }



I also get this error message:


http://localhost/upload/


Fatal error: Call to undefined function form_open_multipart() in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\upload\application\views\main.php on line 14
A PHP Error was encountered
Severity: Error
Message: Call to undefined function form_open_multipart()
Filename: views/main.php
Line Number: 14
Backtrace:


Can anyone help me fix this error?

Thanks in advance.
" If I looks more intelligence please increase my reputation."
Reply
#2

Sounds like you need to load the form_helper:
http://www.codeigniter.com/userguide3/he...elper.html
Reply
#3

I already load:

controllers/upload.php


PHP Code:
<?php


class Upload extends CI_Controller{
    
    public 
function index()
    {
        $this->load->helper('form');    
        $this
->load->view('main');
    }    
    
    
public function upload_image(){
        $this->upload_model->do_upload(); //execute the upload function
        
        
    
}
}



?>


models/upload_model.php

PHP Code:
<?php

class User_model extends CI_Model{
    
 
 var $original_path;
 
 var $resized_path;
 
 var $thumbs_path;
 
 
 //initialize the path where you want to save your images
 
 function __construct(){
 
   parent::__construct();
 
   //return the full path of the directory
 
   //make sure these directories have read and write permessions
 
   $this->original_path realpath(APPPATH.'../uploads/original');
 
   $this->resized_path realpath(APPPATH.'../uploads/resized');
 
   $this->thumbs_path realpath(APPPATH.'../uploads/thumbs');
    
 
 }
 
 
 function do_upload(){
 
   $this->load->library('image_lib');
 
   $config = array(
 
   'allowed_types'     => 'jpg|jpeg|gif|png'//only accept these file types
 
   'max_size'          => 2048//2MB max
 
   'upload_path'       => $this->original_path //upload directory
 
 );
 
 
   $this->load->library('upload'$config);
 
   $image_data $this->upload->data(); //upload the image
 
 
   //your desired config for the resize() function
 
   $config = array(
 
   'source_image'      => $image_data['full_path'], //path to the uploaded image
 
   'new_image'         => $this->resized_path//path to
 
   'maintain_ratio'    => true,
 
   'width'             => 128,
 
   'height'            => 128
    
);
 
 
   //this is the magic line that enables you generate multiple thumbnails
 
   //you have to call the initialize() function each time you call the resize()
 
   //otherwise it will not work and only generate one thumbnail
 
   $this->image_lib->initialize($config);
 
   $this->image_lib->resize();
 
 
   $config = array(
 
   'source_image'      => $image_data['full_path'],
 
   'new_image'         => $this->thumbs_path,
 
   'maintain_ratio'    => true,
 
   'width'             => 36,
 
   'height'            => 36
    
);
 
   //here is the second thumbnail, notice the call for the initialize() function again
 
   $this->image_lib->initialize($config);
 
   $this->image_lib->resize();
 
 }
}

?>


A PHP Error was encountered
Severity: Notice
Message: Undefined property: Upload::$upload_model
Filename: controllers/upload.php
Line Number: 15
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\upload\application\controllers\upload.php
Line: 15
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\upload\index.php
Line: 315
Function: require_once

Fatal error: Call to a member function do_upload() on null in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\upload\application\controllers\upload.php on line 15
A PHP Error was encountered
Severity: Error
Message: Call to a member function do_upload() on null
Filename: controllers/upload.php
Line Number: 15
Backtrace:


upload.php

Line 15:          $this->upload_model->do_upload(); //execute the upload function

That's the error!  Can anyone help me fix the error?
" If I looks more intelligence please increase my reputation."
Reply
#4

There are a number of things wrong here.

I think you need to learn the basics of PHP and of CI and practise debugging yourself.

The error you're getting (if you read it) clearly tells you the problem and where to find it.

"$upload_model" is an undefined property on line 15. So on that line you see that you're calling "$this->upload_model->do_upload();", but CI doesn't know what that model is, because you haven't loaded it.

Also, in your "upload_model.php" file, the class name is "User_model" so you need to update that to conform.

There's a good chance after fixing those things, you'll have other errors. If so, read them carefully and see if you can find the problem yourself.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB