$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..
Related
In CodeIgniter when I upload a file to a particular folder it uploads two copies of the file
To folder. I mean, it repeats the file, I do not know why.
How do I solve this problem?
thank you.
Controller:
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|svg|rar|zip' ;
$config['max_size'] = 220048;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config, 'catalogupload3');
// Create custom object for catalog upload
$this->catalogupload3->initialize($config);
$this->catalogupload3->do_upload('userfile5');
if (!$this->catalogupload3->do_upload('userfile5')){
$url_file = 'nofile' ;
$file_name = 'false';
$file_size = 'false';
}else {
$this->catalogupload3->data();
$url_file = $this->catalogupload3->data('file_name');
$file_name = $_FILES['userfile5']['name'];
$file_size = $_FILES['userfile5']['size'];
}
////
$config['upload_path'] = './files/32/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|svg|rar|zip' ;
$config['max_size'] = 220048;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config, 'catalogupload5');
// Create custom object for catalog upload
$this->catalogupload5->initialize($config);
$this->catalogupload5->do_upload('userfile7');
if (!$this->catalogupload5->do_upload('userfile7')){
$url_file = 'nofile' ;
}else {
$this->catalogupload5->data();
$url_file_32 = $this->catalogupload5->data('file_name');
}
Of course it will upload two copies because you ran the function twice:
// first run
$this->catalogupload3->do_upload('userfile5');
// second run
if (!$this->catalogupload3->do_upload('userfile5'))
If you wanna a check do it once:
// this is enough it will run it and return a bool
if (!$this->catalogupload3->do_upload('userfile5'))
I need to rename image name after upload because my requirement is I need to add id with it so it's possible after store image name in DB.
I have done code for uploading image with and without watermark into different folder but now I want to change it's name like:
$imageName = $imgId.''.randomstring.''.$ext;
I have tried with below code but same image stored in both folder with watermark. I want to store original image in original_image folder and with watermark image in watermark_folder. Please help me to resolve this issue.
$getImgs = $this->photographer_model->get_uploaded_image($result);
$getExt = substr($getImgs[0]['img_name'], strrpos($getImgs[0]['img_name'],'.')+0);
$randomStr = $this->generate_random_string(20);
$fileName = $result."-".$randomStr."".$getExt;
$originalImgPath = base_url()."assets/original_image/".$fileName;
$watermarkImgPath = base_url()."assets/watermark_image/".$fileName;
$uploadOrgImg = 'assets/original_image/'.$fileName;
$uploadWaterImg = 'assets/watermark_image/'.$fileName;
$updateImg = $this->photographer_model->update_img_path($result, $fileName, $originalImgPath, $watermarkImgPath);
if(file_exists('assets/original_image/'.$getImgs[0]['img_name']) || file_exists('assets/watermark_image/'.$getImgs[0]['img_name'])) {
unlink('assets/original_image/'.$getImgs[0]['img_name']);
unlink('assets/watermark_image/'.$getImgs[0]['img_name']);
$config1['upload_path'] = 'assets/original_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['file_name'] = $fileName; // set the name here
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('image');
$config['upload_path'] = 'assets/watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['file_name'] = $fileName; // set the name here
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('image');
}
Update Your Code Below With this
$config1['upload_path'] = 'assets/original_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['encrypt_name'] = TRUE;
And Update for the second one
$config['upload_path'] = 'assets/watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['encrypt_name'] = TRUE;
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.
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
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