I want upload image file on server & want to stored image in database.
How can I store image file in database & also get the image file from database. Please help to solved this problem.
i have done this code in project..
but when uploading image on server upload_path not found.
In controller
$config['upload_path'] = base_url().'aplication/upload/'; // the uploaded images path
$config['allowed_types'] = 'jpg|jpeg|png';/*types of image extentions allowed to be uploaded*/
$config['max_size'] = '3048';// maximum file size that can be uploaded (2MB)
$this->load->library('upload',$config);
if ( ! is_dir($config['upload_path']) ) /* this checks to see if the file path is wrong or does not exist.*/
{
echo( $config['upload_path']);
die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path
$this->load->library('upload',$config); /* this loads codeigniters file upload library*/
}
if ( !$this->upload->do_upload() )
{
$error=array('error'=>$this->upload->display_errors());
//echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error
// $this->load->view('main_view',$error);
}
else
{
$file_data=$this->upload->data();
$data['img']=base_url().'.application/upload/'.$file_data['file_name'];
echo("Image upload successfully..");
//================================================
// $this->load->model('insert_model');
// $this->insert_model->submit_image();
//==========================================
//$this->insert_model->insert_images($this->upload->data());
// $this->load->model('insert_model');
//$this->insert_model->submit_image();
}
That is quite easy. Just create image upload function once in your controller. Here is one function I wrote:
function image_upload($path, $size, $width, $height, $new_name, $ext)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = $size;
$config['file_name'] = time() . $new_name . "." . $ext;
$config['max_width'] = $width;
$config['max_height'] = $height;
$this->load->library('upload', $config); //Load codeigniter's file upload library
if (!$this->upload->do_upload())
{
return false;
} else
{
return $config['file_name'];
}
}
Now in the action function of form where you have file input field call this function as follows do as follows:
if ($_FILES['userfile']['name'] != "")//Check if file was selected by the user
{
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);//Get the extension of file
$picture_name = $this->image_upload("your/upload/directory/", 10000, 10000, 100000, "test", $ext);
//Now save this $picture_name in database
}
else
{
print_r($this->upload->display_errors());//Display errors if file does not get uploaded successfully
}
Thats it.
Read This, then you will get how to upload file.
Now how to store it into DB.!
In controller use this :
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$img_data = array('upload_data' => $this->upload->data());
$full_url = "/uploads/".$img_data['upload_data']['file_name'];
$reply = $this->model_name->upload_image($full_url);
}
In model using upload_image($full_url) function store image name with path, so that you can get back that path whenever needed.
Related
I have done an image upload function for large registration functionality. My uploaded files are stored in a single folder.. but I want to move those files into a separate folder with id stored in the database. is it possible to move those files into separate folders now??
I want to move the uploaded files into a folder with the name of id in the database. I cant do it manually since the uploads folder has many thousands of images. is it possible to do it in code?
public function add_applicants()
{
if (isset($_FILES['photo']) == 1) {
$config['upload_path'] = FCPATH . 'uploads/';
$this->upload_path = $config['upload_path'];
if ($this->validate_upload_path() == TRUE)
{
$config['upload_path'] = $this->upload_path;
}
$config['allowed_types'] = 'jpg';
$config['max_size'] = 2000;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = true;
$config['file_name'] = date('Ymdhis') . '.jpg';
$file_name = $config['file_name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('photo')) {
$this->upload->display_errors();
}
}
$data = $this->input->post();
$data['photo'] = $file_name;
$insetapplicants = $this->Welcome_Model->insertApplicants($data);
if ($insetapplicants == 'Success') {
echo "<script>
alert('New Applicants Added Successfully');
window.location.href='dashboard';
</script>";
}
}
public function index() {
$data['mediumwise_details'] = $this->Welcome_Model->mediumwise_home();
$this->load->view('home',$data);
}
I expect that the files are moved to different folders
For example, my uploads folder has 10 photos of different school_Id stored in the same folder. but I want if 2 photos are from one school_id means, a folder name with that school_id is created and that 2 photos are placed inside that folder. likewise different folder for different school_id 's
I hope the following code will help you. let me know if it does not work.
create on function or method in controller place following code and run that function make necessary changes I have created below code based on the information you have given us.
$result = $this->db->get('mts_applicants')->result_array();
if($result){
foreach($result as $row){
$old_path = FCPATH . 'uploads/';
$new_path = FCPATH . 'uploads/'.$row['school_code'].'/';
$old_name = $old_path.$row['photo'];
$new_name = $new_path.$row['photo'];
if(!is_dir($new_path)){
mkdir($new_path, 0777, true);
}
rename($new_name, $old_name) ;
}
}
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.
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
}
}
I'm trying to upload an image using Codegniter's Upload Library, the image should be uploaded to a directory which is created and returned by the model, do the upload directory changed every time, this is the code I'm supposed to use to change the config upload array:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload');
$this->config->set_item('upload', array('upload_path' => $path));
// I've also used
// $this->config->set_item('upload_path', $path);
if (!$this->upload->do_upload('image'))
echo $this->upload->display_errors();
else {
$image = $this->upload->data();
$data['image'] = $image['file_name'];
}
}
but it doesn't seem to work. The upload_path doesn't change, the image is still uploaded in the path I've specified in config/upload.php file.
Try this:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload', TRUE);
$config = $this->config->item('upload');
$config['upload_path'] = $path;
$this->upload->initialize($config);
//etc.
}
I am currently using codeigniter 2.0 for a project and I have a little problem with thumb creation. I intend to allow users to upload picture and have the picture renamed to their last name then have anoda function from within the upload function create a thumbnail then send it to another directory inside the image directory (I named it avatar). The code does this perfectly, but now I intend to encrypt the last name before changing the uploaded pic name. That is where the problem sets in. The upload is carried out the name is changed but a thumbnail is not created ... please can any one tell me what the problem is here is the code sample
function set_avatar()
{
/*check if user is even logged in */
if($this->session->userdata('user_id'))
{
$last_name=$this->session->userdata('last_name');//this value is encrypted
/*parse the useful user values to the data array for use on this page */
$data['user_id']= $user_id;
$data['email']= $email;
$data['last_name']= $last_name;
$data['first_name']= $first_name;
/*assign an empty value to the error key of the data array to avoid error on the view page*/
$data['error'] = "";
/*the directory for uploading the file the number represents a mixture of birth date and month
af means all files u1 upload 1 avts avatar ..jst a path to make the path undetectable*/
$dir = "./path/avatar";
/*preparing the config settings for upload */
$config['file_name'] = $last_name; // the new file name the image should have
$config['upload_path'] = $dir; // the directory the image should go to,already specified
$config['allowed_types'] = 'gif|jpg|png'; // the file types that are allowed
$config['max_size'] = '10000'; //the maximum image size
$config['max_width'] = '300'; //the maximum image width
$config['max_height'] = '230'; // the maximum image height
$this->load->library('upload', $config); // retrieving the upload library with the specified settings
/*using the do upload settings upload the file if it fails stop upload and show errors*/
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$this->load->view("user/set_avatar",$data);
}
/*else retrieve the upload data from the file uploaded*/
else
{
/*pass all the upload data to the $img_data array*/
$img_data = $this->upload->data();
/*the file(image) extension type png or gif or jpg*/
$img_ext = $img_data['file_ext'];
/*thumb name without extension*/
$thumb = $last_name;
/*thumb name with extension*/
$thumb_name = $thumb.$img_ext;
/*create thumbnail using the thumb nail function*/
$this->create_thumb($thumb_name);
/*record avatar details in the db*/
$avatar_array = array(
"user_id" => $user_id,
"avatar" => $thumb,
"extension" => $img_ext
);
$this->craft_set_user_details->set_avatar($avatar_array);
/*start creat session for img_name and ext*/
$image_session = array(
"img_name"=>$thumb,
"img_ext"=>$img_ext
);
$this->session->set_userdata($image_session);
/*redirect to home page*/
redirect('home','location');
}//end if else $this->upload->do_upload
}//end check if user is logged in ($this->session->userdata('user_id'))
else
{
redirect("index");
}//end if eles user is logged in ($this->session->userdata('user_id'))
}//end function set avatar
/* this is the create_thumb() function */
///start the create thumb function this is used in the set avatar function to create the avatar thumb
function create_thumb($thumb_name)
{
/*check if user is logged in*/
if($this->session->userdata('user_id'))
{
//set the path to were the image is
$dir = "./path/avatar";
//set the path to were the thumb should be sent
$new = "./path/avatar/thumbs";
//set the configuration to be used for image resizing
$config['image_library'] = 'gd2';
$config['source_image'] = $dir;
$config['new_image'] = $new;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 35;
$config['height'] = 50;
//load the image library for image reszing
$this->load->library('image_lib', $config);
//resize image
$this->image_lib->resize();
}//end if eles user is logged in ($this->session->userdata('user_id'))
//else redirect to the home page
else
{
redirect('index','location');
}
}//end function to create
Your source image is a directory. It should be a file path.