image insertion is not working - php

When |I insert the image it is showing the error
Invalid Offset
This is my controller.
I have copied it from the other page in my project and it is working there - but on the new page it is showing the error.
$config['upload_path'] = './assets/upload/'; /* NB! create this dir! */
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '5000';
$config['max_height'] = '5000';
/* Load the upload library */
$this->load->library('upload', $config);
/* Create the config for image library */
/* (pretty self-explanatory) */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
/* Set the height and width or thumbs */
/* Do not worry - CI is pretty smart in resizing */
/* It will create the largest thumb that can fit in those dimensions */
/* Thumbs will be saved in same upload dir but with a _thumb suffix */
/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
$configThumb['width'] = 140;
$configThumb['height'] = 210;
/* Load the image library */
$this->load->library('image_lib');
$file=array();
$file_thumb=array();
/* We have 5 files to upload
* If you want more - change the 6 below as needed
*/
for($i = 1; $i < 3; $i++) {
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$file[$i] = $data['file_name'];
$file_thumb[$i] = $data['raw_name'].'_thumb'.$data['file_ext'];
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
///=============upload end here==================////
echo $file['1'];
exit;

You have set some Image Thumbs
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 140;
$configThumb['height'] = 210;
Ok grate. and you load library as well $this->load->library('image_lib');
so how this settings get config to the library.
Add this too
$this->load->library('image_lib', $configThumb); # configuration variable
Read this
Image Manipulation Class

Related

Rename File Name During Uploading

$uniqId = $this->input->post('U_I_T_Roll_No');
$config['upload_path'] = "./uploads/ProfileImages/";
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '1024';
$config['max_width'] = '1000';
$config['max_height'] = '1000';
/* Load the upload library */
$this->load->library('upload', $config);
/* Create the config for image library */
/* (pretty self-explanatory) */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = FALSE;
$configThumb['maintain_ratio'] = TRUE;
/* Set the height and width or thumbs */
/* Do not worry - CI is pretty smart in resizing */
/* It will create the largest thumb that can fit in those dimensions */
/* Thumbs will be saved in same upload dir but with a _thumb suffix */
/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
$configThumb['width'] = 400;
$configThumb['height'] = 400;
/* Load the image library */
$this->load->library('image_lib');
/* We have 5 files to upload
* If you want more - change the 6 below as needed
*/
for($i = 1; $i < 5; $i++) {
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
$S_image = $_FILES['image1']['name'];
$F_image = $_FILES['image2']['name'];
$M_image = $_FILES['image3']['name'];
$LG_image = $_FILES['image4']['name'];
$data5=array (
'Uniq_Id' => $this->input->post('U_I_T_Roll_No'),
'S_image' => $S_image,
'F_image' => $F_image,
'M_image' => $M_image,
'LG_unique1' => $LG_image,
);
$this->InsertData->studentimageupload($data5);
The Above code Works Fine. I Have 4 Image to Upload and Uploading properly and the name of image saving into database. the problem is the.
I want to Upload image name according to me and save name of the image into database.
Like : S$uniqId, F$uniqId, M$uniqId, LG$uniqId
Do something like this before $this->load->library('upload', $config);
Change $this->load->library('upload', $config); to $this->load->library('upload'); and see below
for($i = 1; $i < 5; $i++) {
$img=$_FILES['image'.$i]['name'];
$new_name=NEW_NAME;
$ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1));
$file_name=$new_name.$ext;
$config['file_name'] = $file_name;
$this->upload->initialize($config);
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
The name used is whatever is present in $_FILES['image1']['name']. If you want to change it, you can change it in the upload form itself. If that is not desirable, you have to call rename() and then update
$S_image = $_FILES['image1']['name'];
$F_image = $_FILES['image2']['name'];
$M_image = $_FILES['image3']['name'];
$LG_image = $_FILES['image4']['name'];
to whatever you have renamed the files to, before storing in database.
I think you can solve the renaming problem by initializing the config each time when you call the do_upload method.
$config['file_name'] = YOUR_OWN_NAME
and then Initialize in your for loop, Just before your do_upload call
$this->upload->initialize($config);
You should be able to specify the 'file_name' after loading the library & right before uploading the file..
//make your config array.
$this->load->library('upload', $config);
for($i=1; $i < 5; $i++)
{
$this->upload->set_filename($path, $filename);
$upload = $this->upload->do_upload('image'. $i);
//do whatever you want to do with the file.
}
I've not used it in any of my projects so far, but it should produce the desired result. Just let me know, if it doesn't work..

Image insertion is working on local server but not on live server

Image insertion is not working on live server but when I am running the same code on local server than its working fine. Please tell me how to do that. It's not even inserting the file name in the database. Is there path problem or anything else?
This is my controller:
$config['upload_path'] = './assets/upload/'; /* NB! create this dir! */
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '5000';
$config['max_height'] = '5000';
/* Load the upload library */
$this->load->library('upload', $config);
/* Create the config for image library */
/* (pretty self-explanatory) */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
/* Set the height and width or thumbs */
/* Do not worry - CI is pretty smart in resizing */
/* It will create the largest thumb that can fit in those dimensions */
/* Thumbs will be saved in same upload dir but with a _thumb suffix */
/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
$configThumb['width'] = 140;
$configThumb['height'] = 210;
/* Load the image library */
$this->load->library('image_lib');
$file=array();
$file_thumb=array();
/* We have 5 files to upload
* If you want more - change the 6 below as needed
*/
for($i = 1; $i < 3; $i++) {
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$file[$i] = $data['file_name'];
$file_thumb[$i] = $data['raw_name'].'_thumb'.$data['file_ext'];
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
Upload ends here.
Upload path on live server may not be writable.

Codeigniter file uploading error in server

I am getting this error:
Could not open /home/Projectname/public_html/assets/uploads/empfiles/
The filetype you are attempting to upload is not allowed.
for reading! File does not exist.
Please help me to resolve this.
I am using Codeigniter version:2.1.4
I have tried this solutions also, but no changes I found.
Uploading in Codeigniter - The filetype you are attempting to upload is not allowed
Code goes here:
$filextesions = $fileparameters->file_extensions;
$path = $fileparameters->path_required;
$filename = $fileparameters->filename;
$inputfilename = $fileparameters->inputname;
$fileType = $fileparameters->$inputfilename['type'];
if($fileparameters->width=='')
{
$width = 0;
}
else
{
$width = $fileparameters->width;
}
if($fileparameters->height=='')
{
$height = 0;
}
else
{
$height = $fileparameters->height;
}
/** Checking for file existence **/
$file_exist= FCPATH.'assets/uploads/'.$path.'/'.$filename;
if(file_exists($file_exist))
{
/* Renaming the existed file name */
$newfilename = uniqid().$filename;
}else
{
$newfilename = $filename;
}
/** Conifgure File parameters **/
$config['upload_path'] = FCPATH.'assets/uploads/'.$path;
$config['allowed_types'] = $filextesions;
$config['file_name'] = $newfilename;
$config['remove_spaces'] = FALSE ;
$config['max_size'] = '0';
$config['max_width'] = $width;
$config['max_height'] = $height;
/** Intialising uploadbject **/
$this->upload->initialize($config);
/** Checking for File existence **/
if($this->upload->do_upload($inputfilename))
{
return $newfilename;
}
else
{
return $this->upload->display_errors();
}
Mime types for xlsx files
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel')
Advance thanks

Codeigniter image manipulator Unable to save the image

Well i have the following problem:
I'm using CodeIgniter 2.1.3 and want to upload an image and create two scaled images. one that is resized to the size of around 50x50 and one that is resized to around 200x200.
Uploading is doing fine.
But when i want to resize the following error occures:
Unable to save the image. Please make sure the image and file directory are writable.
I've set my permission on the server to 777, but it still doesn't compute
Here is the code of the classes:
public function uploadimage()
{
$head = array('title' => 'Upload Image');
$error = array('error' => '');
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
public function changeimage()
{
$config = array();
$config['upload_path'] = 'img/Upload';
$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
$config['max_size'] = '100';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['overwrite'] = TRUE;
$config['file_name'] = md5($this->session->userdata('username')).".png";
$head = array('title' => 'Upload Image');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
else
{
$uploaddata = $this->upload->data();
//Create Thumb
$config2 = array();
$config2['image_library'] = 'gd';
$config2['source_image'] = $uploaddata['full_path'];
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 50;
$config2['height'] = 50;
$config2['new_image'] = '/~3612546/DateSite/img/ProfileThumbs/'.$uploaddata['file_name'];
$this->image_lib->initialize($config2);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
//Create resized original image so images < 200x200 get resized to get as close to 200x200
$config3 = array();
$config3['image_library'] = 'gd';
$config3['source_image'] =$uploaddata['full_path'];
$config3['maintain_ratio'] = TRUE;
$config3['width'] = 200;
$config3['height'] = 200;
$config3['new_image'] = '/~3612546/DateSite/img/ProfileImages/'.$uploaddata['file_name'];
$this->image_lib->initialize($config3);
if ( ! $this->image_lib->resize())
{
echo $uploaddata['full_path'];
echo $this->image_lib->display_errors();
}
}
}
i really don't think tilde is good for upload urls:
/~3612546/DateSite/img/ProfileImages/
then, all your urls are upper/lowercase, put them all to lowercase like this:
/~3612546/dateSite/img/profileimages/
in the end post what error do you receive now that you chmod 777 your upload folder, do you added 777 only to dir or dir+all his content? (it's important dir+all his content to 777)

Codeigniter replace uploaded image

I'm using Codeigniter's File Uploading Class for uploading user avatars. Is there a way to replace a user's image file whenever he/she uploads a new one? I want to replace an existing avatar with the newest one uploaded.
My image uploading controller
function upload_avatar()
{
$config['upload_path'] = './uploads/avatars/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = FALSE; //overwrite user avatar
$config['encrypt_name'] = TRUE;
$config['max_size'] = '200'; //in KB
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect('/settings/avatar');
}
else
{
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib', $config);
$this->image_lib->crop();
//Add image path to database
$avatar_path = 'uploads/avatars/' . $this->upload->file_name;
$user_id = $this->tank_auth->get_user_id();
$this->Settings_model->update_avatar($avatar_path, $user_id);
$this->session->set_flashdata('success', 'Avatar updated!');
redirect('/settings/avatar');
}
}
There is a public attribute overwrite that dictates the decision to overwrite the original file. By default, a new filename is created based on the original. Here's the source from Upload.php in CI:
/*
* Validate the file name
* This function appends an number onto the end of
* the file if one with the same name already exists.
* If it returns false there was a problem.
*/
$this->orig_name = $this->file_name;
if ($this->overwrite == FALSE)
{
$this->file_name = $this->set_filename($this->upload_path, $this->file_name);
if ($this->file_name === FALSE)
{
return FALSE;
}
}
So all you need to do to get the overwrite working is:
$this->load->library('upload', $config);
$this->upload->overwrite = true;
Simple set "overide" be true in your config.
$this->upload->initialize(array(
"upload_path"=>$path,
"allowed_types"=>"jpg|png|jpeg",
"overwrite"=>true
));
did you try to change
$config['overwrite'] = FALSE;
to
$config['overwrite'] = TRUE;

Categories