I am trying to upload single image using codeigniter. But getting error
"Undefined property: Main::$upload
Filename: controllers/Main.php
Line Number: 14"
This is my controller file "controller/Main.php"
<?php
class Main extends CI_Controller
{
public function index(){
$this->load->view('main_view', array('error' => ''));
}
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|png|gif';
$this->load->initialize('upload', $config);
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('main_view', $error);
}
else {
$file_data = $this->upload->data();
$data['img'] = base_url().'/images/'.$file_data['file_name'];
$this->load->view('success_msg', $data);
}
}
}
?>
and this is my view file "views/main_view.php"
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<?php echo $error; ?>
<?php echo form_open_multipart('main/upload');?>
<input type="file" name="userfile" />
<input type="submit" name="Submit" value="upload image"/>
</form>
</body>
</html>
when image upload is successful I'm trying to show the image by this "view/success_msg.php"
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<h1>File has been uploaded.</h1>
<img src="<?php $img?>" width="300" height="300">
</body>
</html>
my image folder name is "images" and it is in the root directory. Please help my why I am having this error ?
Maybe the upload library isn't loaded. Change the first few lines ofpublic function upload() to the following
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|png|gif';
$this->load->library('upload', $config);
By sending $config to load->library you do not need to use initialize().
Controller
public function upload(){
$cc = $_FILES['userfile']['name'];
$extension = pathinfo($cc, PATHINFO_EXTENSION);
$newfile = preg_replace('/[^A-Za-z0-9]/', "", $_FILES["userfile"]['name']);
$config['file_name'] = time() . $newfile;
$ss = ".";
$picture1 = time() . $newfile . $ss . $extension;
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
echo $this->upload->display_errors();
} else {
$this->load->view('success_msg', $picture1 );
}
}
View page
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<?php echo $error; ?>
<?php echo form_open_multipart('main/upload');?>
<input type="file" name="userfile" />
<input type="submit" name="Submit" value="upload image"/>
</form>
</body>
</html>
view/success_msg.php
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<h1>File has been uploaded.</h1>
<img src="<?php $picture1 ?>" width="300" height="300">
</body>
</html>
Related
I'm the newbie in Codeigniter. I put the textbox values to session and go to next page and when i click the back button on 2nd page, then the 1st page data should be still in textbox. How can I do that? Please help me.
Controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$this->load->view('welcome_message');
}
public function page1(){
$this->session->set_userdata('page1',$this->input->get());
$this->config->load('config');
$this->config->set_item('sess_expiration', '60');
$this->load->view('pg2');
}
public function page2(){
$this->session->set_userdata('page2',$this->input->get());
$this->config->load('config');
$this->config->set_item('sess_expiration', '60');
$this->load->view('pg3');
}
public function page3(){
$this->config->load('config');
$this->config->set_item('sess_expiration', '60');
$sess_data=$this->session->userdata();
$ses_key = key($sess_data);
if (empty($this->session->userdata("page1")))
{
echo "Session has been Expired~!";
$this->session->unset_userdata('page1');
$this->session->unset_userdata('page2');
//redirect(site_url(),'refresh');
}
else
{
$q1 = ($sess_data['page1']);
$q2 = ($sess_data['page2']);
$result1 = '';
$result2 = '';
$result3 = '';
$result4 = '';
$result5 = '';
$result6 = '';
$result7 = '';
$result8 = '';
for ($i = 0; $i < count($q1); $i++)
{
$key=key($q1);
$val=$q1[$key];
if ($val<> ' ')
{
if ($key === 'input1')
{
$result1 = $val;
//echo $result1;
}
else if ($key === 'input2')
{
$result2 = $val;
//echo $result2;
}
else if ($key === 'input3')
{
$result3 = $val;
//echo $result3;
}
else
{
$result4 = $val;
//echo $result4;
}
echo $key ." = ". $val ." <br> ";
}
next($q1);
}
for ($i = 0; $i < count($q2); $i++)
{
$key=key($q2);
$val=$q2[$key];
if ($val<> ' ')
{
if ($key === 'input5')
{
$result5 = $val;
//echo $result5;
}
else if ($key === 'input6')
{
$result6 = $val;
//echo $result6;
}
else if ($key === 'input7')
{
$result7 = $val;
//echo $result7;
}
else
{
$result8 = $val;
//echo $result8;
}
echo " <br> <br> " . $key ." = ". $val ." <br> ";
}
next($q2);
}
//$this->testing_model->add_data('sess_table',['val'=>$result1,'val2'=>$result2]);
$this->testing_model->add_data('user_table',['id'=>"default", 'text2'=>$result1, 'text3'=>$result2, 'text4'=>$result3, 'text5'=>$result4]);
$this->testing_model->add_data('sess_table',['id'=>"default", 'val'=>$result5, 'val2'=>$result6, 'val3'=>$result7, 'val4'=>$result8]);
echo "Successfully Saved to Database!";
$this->session->unset_userdata('page1');
$this->session->unset_userdata('page2');
//$querydata=$this->testing_model->query('select * from sess_table');
//print "<pre/>";
// print_r($querydata);
// foreach ($querydata as $key => $value) {
// print_r(json_decode($value->val,true));
// print_r(json_decode($value->val2,true));
// // echo json_decode($value->val.' ---- '.$value->val2;
// }
}
}
}
View 1:
<!DOCTYPE html>
<html lang="en">
</head>
<body>
page 1
<form method="get" action="<?= base_url('welcome/page1') ?>">
input 1 <input name="input1" type="text"/> <br/>
input 2 <input name="input2" type="text"/> <br/>
input 3 <input name="input3" type="text"/> <br/>
input 4 <input name="input4" type="text"/> <br/>
<input type="submit" value="go"/>
</form>
</body>
</html>
View 2:
<!DOCTYPE html>
<html lang="en">
</head>
<body>
page 2
<form method="get" action="<?= base_url('welcome/page2') ?>">
input 1 <input name="input5" type="text" /> <br/>
input 2 <input name="input6" type="text" /> <br/>
input 3 <input name="input7" type="text" /> <br/>
input 4 <input name="input8" type="text" /> <br/>
<input type="submit" value="go"/>
<a type="button" href="<?= base_url('welcome/page2') ?>">Back</a>
</form>
</body>
View 3:
<html>
<head>
<title></title>
</head>
<body>
<form method="get" action="<?= base_url('welcome/page3') ?>">
<input type="submit" value="Save"/>
<a type="button" href="<?= base_url('welcome/page1') ?>">Back</a>
</form>
</body>
</html>
Load library session in construct
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
Open your autoload.php in applications/config/autoload.php
$autoload['libraries'] = array('session');
The session data is the least of your problems.
First off, $this->load->view('pg2'); does not call your other functions in your controller, it loads a view named 'pg2'. In your question you didn't show the names of your views but if 'View 1' is a file named pg1.php in the /views folder, your controller function could look like this:
public function pg1() {
$this->session->set_userdata('page1',$this->input->get());
$this->load->view('pg1');
}
And then you have to learn how to pass data to display in views too... Try to read up on the docs, for example here's the page on how views work: https://www.codeigniter.com/user_guide/general/views.html
How to upload php or html file in codeigniter using upload library? I have controller which working ok when uploading jpg, gif, png files but it won't to upload files with php or html or sql extension.
Here is my controller
public function ipload()
{
$this->load->helper('url');
$this->load->model('m_company');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'php|html|txt';
$config['max_size'] = 2048;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$data = array('error' => $this->upload->display_errors());
}
else
{
$this->upload->do_upload('userfile');
$data = array('upload_data' => $this->upload->data());
$filen = $this->upload->data('file_name');
$this->m_company->set_doc($filen);
}
echo json_encode($data);
}
When allowed files are jpg gif png everything works fine
first you setting controler, you can use access to upload file all class using $config['allowed_types'] ='*';
class c_cutipegawai extends CI_Controller{
function __construct(){
parent::__construct();
}
function do_upload(){
$config['upload_path'] ='./upload/SuratCuti/';
$config['allowed_types'] ='*';
$config['max_size'] =1024*8;
$config['max_widht'] =1024*2;
$config['max_height'] =768;
$this->load->library('upload', $config);
if(! $this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array('upload_data' => $this->upload->data());
}
}
}
After setting in controler, now we craete view to insert button upload file
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('C_CutiPegawai/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
I'm trying to find a trick to save a picture coded in Data-URI like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1MAAAE7CAYAAAA4gNuCAAAgAElEQ…
i/fPjJwSa8k8blB7TPIbffod14w9E/Baq03hUCXPf2/wK6K8SbNo44VwAAAABJRU5ErkJggg==
Another question: Where the picture is saved in the first time in the server? Browser?
JS: save image to user's disk using javascript,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript">
window.onload = function () {
var img = document.getElementById('embedImage');
var button = document.getElementById('saveImage');
img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'+
'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO'+
'9TXL0Y4OHwAAAABJRU5ErkJggg==';
img.onload = function () {
button.removeAttribute('disabled');
};
button.onclick = function () {
window.location.href = img.src.replace('image/png', 'image/octet-stream');
};
};
</script>
</head>
<body>
<img id="embedImage" alt="Red dot"/>
<input id="saveImage" type="button" value="save image" disabled="disabled"/>
</body>
</html>
PHP: http://j-query.blogspot.com/2011/02/save-base64-encoded-canvas-image-to-png.html
<?php
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
EDIT JS solution 2 (fiddle: http://jsfiddle.net/KAdN8/):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript">
var img = document.getElementById('embedImage');
if( document.createEvent ) {
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( 'click', true, false );
img.dispatchEvent(evObj);
} else if( document.createEventObject ) {
img.fireEvent('on'+evt);
}
</script>
</head>
<body>
Download
</body>
</html>
I see CI documentation to create an image uploader.
here is my Controller :
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$this->load->helper(array('form', 'url'));
$this->load->helper('url');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_error();
return FALSE;
}
else
{
$data = $this->upload->data();
return TRUE;
}
}
}
and here is my View
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<form action="upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
But when i upload an image it doesn't upload it and $this->upload->display_error() show nothing(my uploads folder which is in my ci root folder also has 777 permision and i'm using wamp)
Two pointers for you:
<input type="file" name="userfile" size="20" />
Check if the file you're using is not exceeding and the file is being uploaded.
echo $this->upload->display_errors();
See the display_errors with an s.
Suggestions:
In you form's action attribute use a an absolute URL, like:
action="<?php echo base_url() ?>upload/do_upload"
Inside your function do_upload(), do a print_r($_FILEs), to confirm that the file is uploaded correctly with no errors.
I change my function named function do_upload() to function somethingelse() and my problem solved (because i'm calling a built in function)
I am working on a gallery which is supposed to say "Please upload an image" if there are no images in the folder, but next to that also show the image uploaded but these things arent working correctly.
no errors are being shown.
Uploading images and changing them into thumbnails work and they go in the correct folder, but pulling them out is a different story, I have checked these values and they go to the correct destination:
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
and this is my autoload:
$autoload['helper'] = array('url', 'form', 'file');
this is the view where I am suspecting $data is coming out empty?:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Gallery</title>
</head>
<body>
<div id="gallery">
<?php if (isset($data) && count($data)) : ;?>
<?php foreach($images as $image): ?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" />
</div>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an image</div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</body>
</html>
this is my controller:
<?php
class Gallery extends CI_Controller
{
function index()
{
$this->load->model('Gallery_model');
if($this->input->post('upload'))
{
//handle upload
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery', $data);
}
}
?>
this is the model:
<?php
class Gallery_model extends CI_Model{
var $gallery_path;
var $gallery_path_url;
function __construct()
{
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
}
function do_upload()
{
//handle userfile
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
die($this->upload->display_errors());
}
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ratio' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize())
{
die($this->image_lib->display_errors());
}
}
function get_images(){
$files = scandir($this->gallery_path);
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images[] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file,
);
}
}
}
?>
Any help much appreciated!
function get_images(){
/* all the stuff you already have... */
/* return the array! */
return $images;
}