How can i Upload image files only using code igniter? - php

hi i'm trying to upload only image files using code igniter.I need to upload only png,jpg,jpeg files.but other type files also uploaded like .txt,.mp3 etc.my code is given below.please help me to solve this issue as immediately.Thanks to all in advance.
$config['upload_path'] = './avatar/';
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$config['max_size'] = '1024mb';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$ve['data'] = $this->upload->display_errors();
} else {
$this->upload->data();
}

$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);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class:
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}

I think you need to send in the user file/uploaded file while trying to validate it within the if condition.
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$this->upload->data();
}

Related

uploading file in php ci framework neither show errors nor show result

I had seen many videos and took reference from CI user guide but became unable to find out error.When file is submitted from the form,it sends program flow the the uploadImage() method below.Please help me with the way.
Thank you
My code:
public function uploadImage()
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if(!$this->upload->do_upload())
{
$this->load->view('upload');
}
else
{
$this->upload->display_errors();
}
}
I don't know what are you trying to do as plz see below may help..
public function uploadImage()
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
//load upload class library
$this->load->library('upload', $config);
//$this->upload->do_upload('filename') will upload selected file to destiny folder
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('edit', $upload_error);
}
else
{
// case - success
//callback returns an array of data related to the uploaded file like the file name, path, size etc
$upload_data = $this->upload->data();
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' .$upload_data['file_name']. '</strong> was successfully uploaded!</div>';
//$this->load->view('edit_profile', $data);
redirect(base_url("Display_somepage/index"));
}
//this below may be helpful to for debugging
echo $this->image_lib->display_errors();
I think it is a syntax error, notice the arrow.
public function uploadImage()
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('YOUR_FILE_INPUT_NAME')) <=========== HERE
{
$this->load->view('upload');
}
else
{
$this->upload->display_errors();
}
}
Basically, you are telling it to load the view when the uploading succeed, else show errors if the operation fails not the opposite.

uploading file data and textbox ,radio button values to database

i like to upload a file in that form it contain some text boxes and one fileupload button,
how to do it in codeigniter,
when i did that in separately i can but with file upload i cant
function upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|doc|docx';
$config['max_size'] = '1024';
$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('js_home', $error);
}
else
{
$data = array('upload_data' => $this->upload->data(),'phone' =>$this->input->post('phone'),'email' =>$this->input->post('email'),'password' =>$this->input->post('password'));
$this->jobseeker->storefile($data);
$this->load->view('samples/upload_success', $data);// after uploaded the file
}
}
try this
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('js_home', $error);
}
else
{
$fInfo = $this->upload->data();
$data = array('upload_data' => $fInfo['file_name'],'phone' =>$this->input->post('phone'),'email' =>$this->input->post('email'),'password' =>$this->input->post('password'));
$this->jobseeker->storefile($data);
$this->load->view('samples/upload_success', $data);// after uploaded the file
}
in view <input type='file' name='file'>

Codeigniter : Filename didn't appear on database

I just learn to save image data to database, those are filename and path. The path is appear on database, but not with filename. Whats the problem?
This is the 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);
$this->upload_model->upload($config);
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
and the model
function upload ($config) {
$config = $this->upload->data();
$upload_data = array(
'path' => $config['full_path'],
'nama_foto' => $config['file_name']
);
$this->db->insert('tb_picture', $upload_data);
}
and the table
what should i do?
thank you before.
before reading this please try it again yourself, or try any kind of viedotutorial that is on the web http://net.tutsplus.com/sessions/codeigniter-from-scratch/
controller function should look like this
function do_upload()
{
$config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$field_name = "userfile"; //name tag in our HTML form in case you want to change it
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($field_name)) //upload happens
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
//succesful upload get data from upload and use them with our model
$upload_data = $this->upload->data();
$this->upload_model->upload($upload_data);
}
}
model function
function upload ($data) {
if (empty($data) || $data === FALSE) return FALSE;
$insert_data = array(
'path' => $data['full_path'],
'nama_foto' => $data['file_name']
);
$this->db->insert('tb_picture', $insert_data);
return $this->db->insert_id(); //returns last inserted ID
}
Please note that your model was totaly "wrong" you used upload function in it, try to pass data only to it so model can process it.

How to store path image users into a database: Codeigniter

I am trying to upload the paths of the pictures for different users into at database.
I am quite new on codeigniter and I read a lot of tutorials, but I am sill struggling.
This is my controller but I don't even know how to link the image with the user session.
Here is my code:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = false;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}else
{
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('username', $this->input->post('username'));
$this->db->update('users', $profile);
$this->load->view('upload_success', $data);
}
}
My database has a table called users and 'profile_picture' is the field for the path. When I upload an image, I just get all my users fields filled with the same file name.
thanks for the tips guys I solved in this way:
function do_upload(){
if($this->session->userdata('is_logged_in'))
{
$id = $this->session->userdata('id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = false;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view('upload_form', $error);
}else{
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('id', $id);
$this->db->update('users', $profile);
$this->load->view('upload_success', $data);
}
}else{
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("login_view");
$this->load->view("site_footer");
}
}
If your use has an ID, just do imagePath = /upload/avatar/'.$userID.'.jpg' or use a random filename and store it as a new field

CodeIgniter cyrillic names in files

$config['upload_path'] = './uploads/logo/';
$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('image_bg'))
{
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
echo 'fail';
}
else
{
//some actions
}
when i upload a file with cyrillic names, they look worse, how can I convert a file name to a windows-1251 unicode after uploading it to the server in codeigniter?
set the file name in config.
$config['file_name'] = 'image_' . time();
$config[0]['upload_path'] = './uploaded_files/banner';
$config[0]['allowed_types'] = 'gif|jpg|png';
$config[0]['file_name'] = $image_id."_ipad";
$config[0]['remove_spaces'] = TRUE;
$config[0]['overwrite'] = TRUE;
//Loading Library - File Uploading
$this->load->library('upload');
$this->upload->initialize($config[0]);
$this->upload->do_upload('banner_image_ipad');
$data = array('upload_data' => $this->upload->data());
$banner_image_ipad = $data['upload_data']['file_name'];

Categories