codeigniter upload function not uploading to directory - codeigniter

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

Related

How to remove existing file when uploading new one in codeigniter file uploading?

I am using codeigniter file uploading class to upload files and images in my local folder.I am using the code below, it's working fine.
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|txt|sql';
$config['max_size'] = 8048000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('Emp_details_view', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Now, my question is how to remove previously added file when uploading new one.
You can use unlink() function to delete your previous image
unlink('image-path/image-name');
first save your filename in a database table then when unlinking take that value from database and unlink. here uploads is the foldername .
unlink("uploads/".$val['image']);
$val['image'] contains imagename from db.

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

How to store uploaded file into folder using CodeIgniter?

How to store upload file into folder using CodeIgniter?
Please Be sure with your gallery folder it must be in root alongside with application folder name gallery
$config['upload_path']='./gallery/';
$config['allowed_types']='gif|png|jpeg|jpg';
$config['max_size']='100';
$config['max_width']='1024';
$config['max_height']='700';
$this->upload->initialize($config);
$this->upload->do_upload('upload_photo');
Hey try this code inc controller. This the function when u hit the upload button in your vie form..
function sendresume()
{
$config['upload_path'] = 'C:\xampp\htdocs\upload\application'; //path where to save in the systme
$config['allowed_types'] = 'doc|docx|pdf'; //file types to accept while uplaoding
$config['max_size'] = '10240'; //size limit
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo "Resume Successfully uploaded to database.............";
$file = $data['upload_data']['full_path']; //upload file path
}
}

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

photo upload with codeigniter

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

Resources