I am working on a user registration form in which the user is required to upload his profile image and a document for verification purposes. These are the two different fields in the form. The code works fine but the problem is that it only uploads the profile image and stores it in the verification documents folder. It does not upload the verification document. It works fine if I remove or comment one of the field.
Below is my controller function.
/** UPLOAD PROFILE IMAGE **/
$prof_pic = $_FILES['profile_pic']['name'];
$config = array ('upload_path' => './images/tutors/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
/** UPLOAD VERIFICATION DOCUMENT **/
$tut_ver_docs = $_FILES['tut_ver_docs']['name'];
$new_config = array ('upload_path' => './emp_verification_documents/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);
$this->load->library('upload', $new_config);
$this->upload->do_upload('tut_ver_docs');
Try replacing:
$this->load->library('upload', $config);
with :
$this->load->library('upload');
$this->upload->initialize($config);
replace
$this->load->library('upload', $new_config);
with
$this->upload->initialize($new_config);
I'm not entirely sure why you're loading the upload class twice:
$this->load->library('upload');
$this->upload->upload_path = './images/tutors/';
$this->upload->allowed_types = array('jpeg','jpg','png');
foreach($_FILES as $name => $file){
if($name == 'tut_ver_docs'){ //if its the verification document, change the upload path
$this->upload->upload_path = './emp_verification_documents/';
}
if( ! $this->upload->do_upload($name){
//catch error
}
}
I did not tested it But I think this should work for you
/** UPLOAD PROFILE IMAGE **/
$this->load->library('upload');//load the library
$config = array ('upload_path' => './images/tutors/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);//config for profile picture
$this->upload->initialize($config);
if (!$this->upload->do_upload('profile_pic'))
{
print_r($this->upload->display_errors());//upload fails.you can print the error or display somewhere else
}
/** UPLOAD VERIFICATION DOCUMENT **/
$tut_ver_docs = $_FILES['tut_ver_docs']['name'];
$new_config = array ('upload_path' => './emp_verification_documents/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);
$this->upload->initialize($new_config);
if (!$this->upload->do_upload('tut_ver_docs'))
{
print_r($this->upload->display_errors());//upload fails
}
Related
I am try to upload a image by php codeigniter but show "The upload path does not appear to be valid."
This Action is performed in xampp server and window 10
public function do_upload($username) {
$config = array(
'upload_path' => "./assets/images/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
print_r($data);
} else {
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
This code i used i chek many time but i not found error
What error in the code,
If i use this same code in other project it will work but not in the main project , Please help, Thanks in advance
In CodeIgniter you should always try and use the constant APPPATH to find the application folder.
APPPATH is the best because it dynamically retrieves the proper filesystem directory structure no matter which operating system you are on. This ensures that all file paths in your code are going to be correct.
However if you are not trying to get to the Application folder. You can use the FCPATH constant which dynamically points to the front controller of your application.
You are getting this error because the path to your /assets/images folder is not valid. You need to MAKE SURE that the path is correct.
first you should check you have created folder assets/images folder in root directory because in config this path is defined
$config = [
'upload_path' => "./assets/images/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
];
$this->load->library('upload',$config);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div style="color:red;" class="error">', '</div>');
if$this->upload->do_upload()){
$img = $this->upload->data();
$img_name = base_url('uploads/'.$img['file_name']);
$data = $this->input->post();
$data['image_path'] = $img_name;
unset($data['submit']);
return $this->flash_message(
$this->articlemodel->add_article($data),
'Article added sucessfully',
'Article added to failed'
);
}
else
{
$upload_error = $this->upload->display_errors;
}
It is very simple to resolve
If directory inside the application folder
application -> assets -> images
'upload_path' => APPPATH. 'assets/images/';
If directory outside of the application folder
root folder -> assets -> images
'upload_path' => FCPATH. 'assets/images/';
A Problem was encountered while attempting to move the uploaded file to the final destination
Unable to upload the file in codeigniter using their upload class, upload directory is also exist,
below is a code
$config = array(
'upload_path' => './upload_dir/users_docs/',
'allowed_types' => 'gif|png|jpeg',
'max_size' => 0,
'max_width' => 0,
'max_height' => 0,
'file_name' => $this->currTime,
);
$this->load->library('upload', $config);
if (!empty($_FILES['company_logo']['name']) && !$this->upload->do_upload('company_logo')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error_message', $error['error']);
redirect('home');
}
$data = array('upload_data' => $this->upload->data());
There was an issue in the 'file_name' => $this->currTime, the format of time was not correct, Apostrophe where coming in the filename var, I just changed it to something else, now it's working, Thank you
I working on a project with PHP Codeigniter. I want to upload an image, change the name of the image and store it in database (with extension). I have written the following code. It works perfectly but the problem is that the image name is stored without its extension in database. For example, when I run the code, the name that is stored in database is 'imagename', instead of 'imagename.jpg' or 'imagename.png'. I want to store the complete name with extension.
This is my Controller
public function FunctionName()
{
$fname = ucwords($this->input->post('fname'));
$lname = ucwords($this->input->post('lname'));
$prof_pic = $_FILES['profile_pic']['name'];
$config = array ('upload_path' => './images/students/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'file_name' => $fname."_".$lname,
'remove_spaces' => TRUE,
'file_ext_tolower' => TRUE
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
$image_name = $config['file_name'].".".$config['file_type'];
$data = array('std_fname' => $std_fname,
'std_lname' => $std_lname,
'profile_pic' => $image
);
$this->General_Model->create_record($data, "table_name");
}
To get uploaded file name use
$this->upload->data('file_name');
AS
$this->upload->do_upload('profile_pic');
$image_name = $this->upload->data('file_name');
Read Information about the uploaded file
Try with the below code
$prof_pic = $_FILES['profile_pic']['name'];
$newName = "<Desired name>".".".pathinfo($prof_pic, PATHINFO_EXTENSION);
$config = array ('upload_path' => './images/students/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'file_name' => $newName,
'remove_spaces' => TRUE,
'file_ext_tolower' => TRUE
);
the line
$newName = "<Desired name>".".".pathinfo($prof_pic, PATHINFO_EXTENSION);
Will assign the new name with the file extension of the actual file
I am using CodeIgniter file uploading class to upload a image. But if I try and select a pdf instead of image I do not get any errors on form submission.
relevant code
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->article_path.'/magazine',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$this->upload->display_errors();
$image_data = $this->upload->data();
It is because you never went to the documentation (here), please do so in order to perform better in CodeIgniter framework.
this should give you heads up (please read comments)
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->article_path.'/magazine',
'max_size' => 2000
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) //important!
{
// something went really wrong show error page
$error = array('error' => $this->upload->display_errors()); //associate view variable $error with upload errors
$this->load->view('upload_form', $error); //show error page
}
else
{
//all is good we upload
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
This is what im trying to do in codeigniter:
I have member profile pages that all have their own gallery of photos. The user to be able to login and upload photos. I want the photos to then be placed inside of a folder with the usersname. So if the directory doesnt exsit for the user i want it to be created once he uploads a photo. The last thing of this is I want those photos uploaded to relate to that user and display on his profile page.
What I have working:
I can upload photos but it goes inside of the regular upload folder, I can also see the filename inside of the database that relates to that photo and I can view the files.
I need the user photos to be created inside of a folder with the username or id. and only the user photos to be displayed on his page. here is my gallery model:
<?php
class Gallery_model extends CI_Model{
// initalizes the gallery path variable
var $gallery_path;
var $gallery_path_url;
public function __construct(){
parent::__construct();
//sets the gallery path to applications folder and gallery image path
$this->gallery_path = realpath(APPPATH . '../portfolio');
$this->gallery_path_url = base_url().'portfolio/';
}
function do_upload(){
$config = array(
// requried variable allowed types is needed also delcared the upload path to gallery path
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 3000
);
// loads the library and sets where its going to go to
$this->load->library('upload',$config);
// this performs the upload operation
$this->upload->do_upload();
//returns data about upload ( file location )
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 250,
'height' => 220
);
$data = array(
'username' => $this->input->post('username'),
'category' => $this->input->post('category'),
'filename' => $image_data['file_name'],
);
$this->db->insert('portfolio', $data );
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images(){
$files = scandir($this->gallery_path);
// substracts these out of array
$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
);
}
return $images;
}
}
and here is my view:
function gallery(){
$this->load->model('user_model');
$data = array(
//$session_id = $this->session->userdata('session_id')
); // if no data is there and you wont get an error
if($query = $this->user_model->get_profile())
{
$data['records'] = $query;
}
// loads the model. if the upload posts, call the do model method from the gallery model Model.
$this->load->model('gallery_model');
if ($this->input->post('upload')){
$this->gallery_model->do_upload();
}
// displays the images
$data['images'] = $this->gallery_model->get_images();
$data['main_content'] = '/pages/gallery';
$this->load->view('templates/template',$data);
}
You see the original filename in the portfolio folder because you missed out the "file_name" config variable for the upload library. Example:
$config = array(
// requried variable allowed types is needed also delcared the upload path to gallery path
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 3000,
'file_name' => "the user id or username"
);
I also see that you do not have "overwrite" variable set. Can be an problem because you are storing the filename as username as CI will not overwrite the file when the user upload a new photo.