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.
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/';
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 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 was trying to upload an image and create multiple resized copies of that image to be used in different locations in a website. I was using the CodeIgniter Image Manipulation Class and it worked perfectly and I was able to get a resized image, however when I tried to create more than one resized image it didn’t work.
//controller
<?php
class Upload extends CI_Controller{
public function upload_image(){
$this->upload_model->do_upload(); //execute the upload function
}
}
//model
<?php
class User_model extends CI_Model{
var $original_path;
var $resized_path;
var $thumbs_path;
//initialize the path where you want to save your images
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
// desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
when i am using CI Image manipulation Class, I thought that calling the resize function twice will produce two resized images but that didn’t actually work and after some research and tutorials I found out that I have to initialize the image_lib class every time I call the resize() function.
So Your function should look like this
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
desires $config array
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
<?php
$image = array(
'name' => 'userfile',
'id' => 'userfile',
);
$submit = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Upload'
);
?>
<?php echo form_open_multipart('upload/upload_image', 'id=upload_file'); ?> <!-- must autoload form helper for this -->
<?php echo form_upload($image); ?>
<?php echo form_submit($submit); ?>
<?php echo form_close(); ?>
this is main.php in view folder
<?php
class Upload_model extends CI_Model{
var $original_path;
var $resized_path;
var $thumbs_path;
//initialize the path where you want to save your images
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
print_r($image_data);
if($image_data){
echo "uploaded";
}else {echo "not upload";}
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
this is my model
and controller only load main.php that allow me select image click submit and then controller call model above to load image
the problem is --> there are no error generate but image and thumbs not uploaded to directory why please?