Codeigniter file uploading error in server - php

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

Related

Repeat the file to 2 copise while upload codeigniter

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'))

Trying to upload multiple files in codeigniter. Getting do_upload expects a sting array given error

I am trying to upload multiple files in CodeIgniter. But I am getting below warning and error
A PHP Error was encountered
Severity: Warning
Message: is_uploaded_file() expects parameter 1 to be string, array given
Filename: libraries/Upload.php
Error: You did not select a file to upload.
Here is my file upload form control:
<input type="file" accept="image/png, image/jpeg, image/gif, application/pdf, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, text/plain, application/pdf" name="file[]" multiple/>
My Controller function
Here $file is the name of the file being uploaded which I pass to this file_upload() from other function.
public function file_upload($file){
$new_file = "";
$original_file_name = '';
if($file!=""){
$file_name = $file;
$original_file_name = $file_name;
$random = rand(1, 10000000000000000);
$makeRandom = hash('sha512', $random.$this->input->post('title') . config_item("encryption_key"));
$file_name_rename = $makeRandom;
$explode = explode('.', $file_name);
if(count($explode) >= 2) {
$new_file = $file_name_rename.'.'.$explode[1];
$config['upload_path'] = "./uploads/images";
$config['allowed_types'] = "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx|txt|ppt|csv";
$config['file_name'] = $new_file;
$config['max_size'] = '3072';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload',$config);
$this->upload->initialize($config);
if(!$this->upload->do_upload("file")) {
print_r($this->upload->display_errors());
} else {
echo "success";
}
} else {
//error
}
}else{
//some code here
}
}
imho your code doesn't make any sense - but the main problem here is - you've to change the _FILES array
Something like that should work
public function file_upload()
{
$strInputFileName = "file";
$arrFiles = $_FILES;
$config['upload_path'] = "./uploads/images";
$config['allowed_types'] = "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx|txt|ppt|csv";
$config['file_name'] = $new_file;
$config['max_size'] = '3072';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$config['file_name'] = $this->getRandomFileName();
if (is_array($_FILES[$strInputFileName]['name']))
{
$countFiles = count($_FILES[$strInputFileName]['name']);
for($i=0;$i<$countFiles; $i++)
{
//overwrite _FILES array
$_FILES[$strInputFileName]['name'] = $arrFiles[$strInputFileName]['name'][$i];
$_FILES[$strInputFileName]['type'] = $arrFiles[$strInputFileName]['type'][$i];
$_FILES[$strInputFileName]['tmp_name'] = $arrFiles$strInputFileName]['tmp_name'][$i];
$_FILES[$strInputFileName]['error'] = $arrFiles[$strInputFileName]['error'][$i];
$_FILES[$strInputFileName]['size'] = $arrFiles[$strInputFileName]['size'][$i];
$this->upload->initialize($config);
if(!$this->upload->do_upload($strInputFileName))
{
print_r($this->upload->display_errors());
}
else
{
echo "success";
}
}
}
else
{
$this->upload->initialize($config);
if(!$this->upload->do_upload($strInputFileName))
{
print_r($this->upload->display_errors());
}
else
{
echo "success";
}
}
}
private function getRandomFileName()
{
$random = rand(1, 10000000000000000);
return hash('sha512', $random.$this->input->post('title') . config_item("encryption_key"));
}

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..

Dynamically Codeigniter Image Upload Rename

I am uploading multiple dynamic images with codeigniter with the following code.
foreach ($_FILES as $key => $value) {
$imagePrefix = time();
if (!$this->upload->do_upload($key))
{
echo $errors = $this->upload->display_errors();
return '';
}
else
{
$imagename = $imagePrefix.$value['name'];
$insertImage = $this->db->query("Insert into `tbl_event_imges` (`iEventID`,`vImage`) values ('".$insertId."','".$imagename."')");
}
}
When I upload an image to the specific folder this works fine; the issue is if a user uploads an image having same name, then it will automatically rename the image and upload to the folder, but what I want is to rename it by adding $imagePrefix that I have added in else part of the code and then want to upload the image with this name. But this is not working with me..
Any suggestion please?
you need to provide configuration preferences to your upload function like so:
$imagePrefix = time();
$imagename = $imagePrefix.$value['name'];
$this->load->library('upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename; // set the name here
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)){
...
}else{
...
}
Source: http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload#setting-preferences
Just alternative, you can use CI's rename function to rename after file uploaded.
$image = $this->upload->data();
$file_path = $image ['file_path'];
$file = $image ['full_path'];
$file_ext = $image['file_ext'];
$final_file_name = time() . $image['file_name'] . $file_ext;
only add below code and it run successfully and also set encrypt_name to false
$path = $_FILES['userfile']['name'];
$newName = "Add any name".".".pathinfo($path, PATHINFO_EXTENSION);
$config['file_name'] = $newName;
$config['encrypt_name'] = false;

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