photo upload with codeigniter - image

I know there are many tutorials online, but I could not make them work :( maybe something particularly wrong with my system :/
My Controller localpath is:
/localhost/rl/applications/backend/controller/
Controller:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_image', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['id'] = $this->input->post['id_work'];
$this->load->view('add_image', $data);
}
}
My View localpath is:
/localhost/rl/applications/backend/view/
View:
echo form_open_multipart('do_upload');
<ul class="frm">
<li><label>File: *</label><input type="file" name="userfile" class="frmlmnt" size="50" /></li>
<li><label></label><input type="submit" class="btn" value="Upload" /></li>
</ul>
</form>
Maybe I do something wrong with path

Change your view
echo form_open_multipart('backend/controllername/do_upload');
and create backend folder inside the controllers folder.

I have no idea what codeigniter is, but I see three things right off:
Your function doesn't have anything passed to it. I'm not sure if the "this" variable handles that, but maybe you should pass it the upload URL?
You have config['size'] set to '100'. I can only guess that size refers to the filesize, but does this extension you are using default to KB or MB?
You have this bit:
$this->load->library('upload', $config);
and this bit:
$data = array('upload_data' => $this->upload->data());
$data['id'] = $this->input->post['id_work'];
$this->load->view('add_image', $data);
Which part is actually doing the uploading? If it's the first one, how does it know what to upload? I don't see that in any of the config array, and imagine "upload" is not the address of the file...
If it's the second part, you have it set to
$this->load->view('add_image', $data);
But why would you want to view it? Aren't you uploading it? I'd assume that view IS what uploads it, but you use the same method for errors in the previous part.

hey Anthony, thanks for giving time to write such detailed answer! it helped me to think over some parts :)
and later I found a great tutorial which makes exactly what i need :) and everything is working great now. thanks for help ;)
tut link which helped me:
http://net.tutsplus.com/videos/screencasts/easy-development-with-codeigniter/#comment-83378

Related

Upload Image Using Path Codeigniter

I am facing problem in my uploading file using codeigniter.When I create a leave request and upload the image, the image did not appear in the folder path and also did not have a path at the database. Actually, I am doing Leave Request Management I already insert coding upload in my controller. This is my controller leaves.php
Controller (leaves.php)
public function upload ()
{
$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('create', $error);
}
else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
$this->load->view('view', $data);
}
This is my view. Actually I has 2 view that called create.php and view.php.
Create.php is request leave and also for uploading file and for view.php is when user want to view their leave request and also the uploading image. This is my code for both view.
View(create.php)
<? echo form_open_multipart('create/upload');?>
<input type="file" name="image" class=" inputfile" value="<?php echo set_value ('image'); ?>" />
View(view.php)
<label for="image"><?php echo lang ('leaves_view_field_image'); ?>Uploaded File</label>
<img src="?php echo $leave['image']"><?php echo $leave['image']; ?>
When I upload the image with the leave request, the image uploading only fetch name file not the whole file in the database. Can someone help me ?
public function upload ()
{
$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('create', $error);
}
else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
$this->load->view('view', $data);
}
}
Note: make sure folder read/write permission give in linux system
Here is the error if ( ! $this->upload->do_upload()) change it to if ( ! $this->upload->do_upload('image')) where "image" is the same thing as "$_FILES['image']" that is the val in your html e.g <input type="file" name="image">.
I hope this will help you to upload you file.

Upload Multiple Image when code moved to another laptop it shows error

I did my best to debug this code in my first laptop and successfully worked it out but now when i move it to another laptop it suddenly began to show some errors again which i cannot understand the reason why.
Controller
public function uploadCollateralImage_Front()
{
$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2408';
$config['max_width'] = '2408';
$config['max_height'] = '2408';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1')){
$error = array('error'.'hey' => $this->upload->display_errors());
var_dump( $error); die;
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
// var_dump($post_image); die;
return $post_image;
}
}
View
<?php
$attributes = array('name' => 'applicationform');
$hidden = array('userfile1' => 'userfile1');
echo form_open_multipart("Client_Dashboard/applicationPost",$attributes, $hidden);
?>
Front-view Image
<input type="file" name="userfile1" class="w3-margin-left w3-col s10 w3-teal w3-medium w3-hover-white w3-wide " required/>
and my main method
public function applicationPost(){
$data_vehicleinformation = array();
$data_vehicleinformation['FrontImage'] = $this->uploadCollateralImage_Front();
check_insert_user = $this->foo_app->apply_loan($data_log, $data_user,
$data_loanapplication, $data_collateraldetails_app, $data_vehicleinformation ,
$data_paymentdetails, $data_loanpayment, $data_loanapproval,
$data_loanrequest);
}
it show some error like
array(1) { ["errorhey"]=> string(43) "
You did not select a file to upload.
" }
which totally means it did not end up saving in database. and i thought that my DB is the error maybe because of the collation or what other matter but its not it only show the error from function uploadCollateralImage()
as by your comment:
moved CI installation from PHP Version 5.6.23-windows8 to PHP Version
5.5.11-Windows7 Ultimate
the Codeigniter 3.x manual says:
Server Requirements
PHP version 5.6 or newer is recommended.
please check all server requirements here

codeigniter upload function not uploading to directory

I've followed the codeigniter example when it comes to uploading a file, but for some reason it is not working. I've created the folder uploads under public_html folder immediately. I've checked also the flow of the code using echo statements, and it is reaching within the if statement shown below, yet still nothing is showing in the directory uploads which is set to permission 0777.
if($_FILES)
{
//handling the upload
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '480';
$config['max_height'] = '270';
$this->load->library('upload', $config);
if ( ! $this->courseImage())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('new/image', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('error', $data);
}
}else{
$this->load->view('new/image');
}
Appreciate your support and I am still learning codeigniter, so forgive me if it is too easy of a problem.
I did something else and this worked for me. I followed the exact function names provided in codeigniter docs and it worked. I created a method called do_upload and called it using $this->do_upload. This actually worked well.
Thanks

CodeIgniter file upload black hole

I am trying to upload a single file in CodeIgniter 2.0.2 in a normal way.
The do_upload() function returns true but no file appears in the directory.
Directory permissions appear to be fine (after running other diagnostics not shown in code below), so it makes no sense that the file is not appearing in the upload directory.
Here is controller and view code, adapted straight from the CI docs.
Controller:
function do_upload_sql(){
// create directory
if (! is_dir(BACKUP_DIR)) {
mkdir(BACKUP_DIR, 0777);
}
// or if it exists and has restricted file permissions, change them
elseif(fileperms(BACKUP_DIR)!=0777){
chmod(BACKUP_DIR, 0777);
}
$config['upload_path'] = BACKUP_DIR;
$config['allowed_types'] = 'backup'; // an SQL backup file type
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
//$this->upload->initialize($config); //redundant, so commenting out
if ( ! $this->upload->do_upload())
{
$data['action'] = 'c_backup_restore/do_upload_sql';
$tab_error = array('error' => $this->upload->display_errors());
$data['error'] = $tab_error['error'];
$this->load->view('common/header');
$this->load->view('v_upload_sql', $data);
}
else
{
echo "success"; // yes it's getting here, but i get no file!
$data = array('upload_data' => $this->upload->data());
$file_upload = $data["upload_data"];
$this->restore_backup($file_upload); // go do something useful with the file
}
}
View:
<p>Select the backup file</p>
<div class="not_success"><?php echo $error;?></div>
<?php echo form_open_multipart($action);?>
<input type="file" name="userfile" size="30" />
<input type="submit" value="Upload" />
</form>
I found the bug using the xDebug profiler.
It turned out that the native php function chdir(path) further downstream was the problem.
#qwertzman's link to paths in CI may prove to be helpful to determining exactly why the hangup occurs.

uploading file to database

hi its my first time here. i really dont know what's wrong with my codes. iam trying to upload file to database yet when i click the upload button, an error would occur.. (object not found) hope someone can help me...
btw, heres my code snippet
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt|pdf';
$config['max_size'] = '5000';
$config['max_width'] = '2500';
$config['max_height'] = '2500';
$config['remove_spaces']= 'true';
$this->load->library('upload', $config);
$data= array('userfile' => $this->upload->do_upload());
//DEFINE POSTED FILE INTO VARIABLE
$name= $data['userfile']['name'];
$tmpname= $data['userfile']['tmpname'];
$type= $data['userfile']['type'];
$size= $data['userfile']['size'];
//OPEN FILE AND EXTRACT DATA /CONTENT FROM IT
$fp = fopen($tmpname, 'r');
$content= fread($fp, $size($tmpname));
$content= addslashes($content);
fclose($fp);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
}
else
{
$data = array('userfile' => $this->upload->data());
$this->load->database();
$ret=$this->db->insert($name, $type, $size, $content);
unlink($tmpname);
//$this->load->database();
//$this->load->view('upload', $data);
}
}
The $this->db->insert() method does not work this way. It takes two arguments: the first one is the table into which you want to insert your data, the second is an array containing your data.
In your case, you should first put your file's data into an array :
$file_data=array('name'=>$name,'type'=>$type,'size'=>$size,'content'=>$content)
and then insert that into the appropriate table. I used files as an example. Use the one you actually need.
$ret=$this->db->insert('files',$file_data);
Please note though that except in some rare cases (file writing forbidden, etc...) it is generally a better idea to save files as ... files

Resources