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
Related
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 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
}
I am working on a codeigniter project where users can create profiles and upload their images..I want to create an image thumbnail (200 X 200) and save it in the directory. My code works perfectly as it uploads the image and also stores the image name in database but the problem is it does not resize the image..I want to resize all images (200 X 200) and then store them in directory.
This is 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' => $Maxtype."_".$fname."_".$lname,
'remove_spaces' => TRUE,
'image_library' => gd2,
'source_image' => $prof_pic,
'new_image' => './images/students/',
'maintain_ratio' => TRUE,
'height' => 200,
'width' => 200
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
$extension = pathinfo($prof_pic);
$ext = $extension['extension'];
$image = $config['file_name'].".".$ext;
$data = array('std_fname' => $fname,
'std_lname' => $lname,
'profile_pic' => $image
);
}
I want to have a multiple file upload code.
For example:
Koala.jpg
Penguins.jpg
Jellyfish.jpg
There is input text where the user can set the new name of the image.
The user will now upload the images and the inputted text for new image name is "Animals"
Now, what I want is when this uploaded the output should be Animals1.jpg, Animals2.jpg, Animals3.jpg.
The problem is when I tried to upload all these images, only one image is uploading.
I tried to make research and applied some codes on my program, but still not working.
Controller
public function do_upload() {
$config = array(
'image_library' => 'gd2',
'file_name' => $this->input->post('finame'),
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|jpeg',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
$error = array('error' => $error_msg);
}
else {
$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => $data['thumbnail_name'],
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'author' => $this->session->userdata('username'),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
//'document_name' => $field,
//'department' => $field2,
//'notes' => "",
);
$this->session->set_userdata('image_print', $file_array);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$user_level['records']=$this->user_model->get_records();
$this->load->view('v_dashboard/page/header_view', $user_level);
$this->load->view('v_document/upload/upload_result_view', $data);
$this->load->view('v_dashboard/page/footer_view');
}
}
I have this on my HTML
<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>
By the way, I'm using BLOB on my database.
I tried to refer to this links
Ellislab
GitHub
StackOverflow
StackOverflow
CodingLikeASir
You have to run the for loop till the count of the uploaded image file.
Like this:
for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{
// function to add the image name one by one into database.
}
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.