image not storing in folder using codeigniter - codeigniter

My code is given below i unable to store image in sepecfic folder in codeigniter. Image is storing database table but not store in the specific path.
Controller:
if (isset($_FILES['header_image']) && $_FILES['header_image']['name'] != "") {
$fileName="header_image_$id";
$ext= end(explode('.',$_FILES['header_image']['name']));
$file_name=$fileName.".".$ext;
$this->_upload('uploads/header_images/', "gif|jpg|png|jpeg", "header_image",$file_name);
$this->generic_model->updateRecord("blood_tips",array("header_image"=>'uploads/header_images/'.$fileName.".$ext"), array("id"=>$id));
}
private function _upload($uploadPath,$allowedTypes,$name,$file_name)
{
$config['file_name'] = $file_name;
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = $allowedTypes;
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload($name))
{
return $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}

Your file upload parameter is different than proposed documentation .
$config['upload_path'] = './uploads/'; Notice "./" before path.

te correct way to upload file in CI, you need to make like this:
$fileName="header_image_$id";
$ext= end(explode('.',$_FILES['header_image']['name']));
$config['upload_path'] = './uploads/header_images/';
$config['file_name'] = $fileName.".".$ext;//The extension provided in the file name must also be an allowed file type.
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}

Related

codeigniter upload images from different inputs

my code is working fine in the single upload, but I want to make a loop for my code that I won't repeatedly create a function over and over because of using two different inputs.
public function uploadImage_1()
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1')){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
public function uploadImage_2()
{}
` View
<input type="file" name="userfile1" size="20" required/>
<input type="file" name="userfile2" size="20" required/>`
You can pass the input field name as parameter into your function.
like this:
public function uploadImage($input_field_name)
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload($input_field_name)){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
Now use the above function like this:
// These variables stores the name of your files
$fileName_1 = $this->uploadImage('userfile1');
$fileName_2 = $this->uploadImage('userfile2');

While Uploading Image don't want to save actual image name with space

In codeigniter while I uploading the image and store image name in database If actual image name is like "abc data.jpg" and I want to store image name like abc_data.jpg and also with this name image should move in uploads folder.
This is my image Upload Code:-
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('cover_image'))
{
$data =$this->upload->data();
$data_ary = array(
'project_id' => (int)$iProjectId,
'image_url' => $data['file_name'],
'is_covred_photo' => 'YES'
);
$this->db_project->insert( $this->sTable6 , $data_ary);
$aResp = array( 'project_images_id' => $this->db->insert_id());
}
Add $config['file_name']
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$config['file_name'] = 'My_new_file_name'; # Add here
$this->load->library('upload', $config);
remove this line as well
$this->upload->initialize($config);
Try this:
$file_name = $_FILES['file_view_name']['name'];
$file_name_pieces = split('_', $file_name);
$new_file_name = '';
$count = 1;
foreach($file_name_pieces as $piece)
{
if ($count !== 1)
{
$piece = ucfirst($piece);
}
$new_file_name .= $piece;
$count++;
}
$config['file_name'] = $new_file_name;
Refer this

The filetype you are attempting to upload is not allowed ods file in codeigniter

i want to upload an ods file ,but its showing an error:
The filetype you are attempting to upload is not allowed
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'ods|csv|xls';
// $config['max_size'] = '';
$config['max_width'] = '';
$config['max_height'] = '';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadstatement',$error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data1=$this->upload->data();
$csv_file1=$data1['file_name'];
added/replaced the following line to the mime types file application/config/mimes.php
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

My file type not saving on my database

I have completed file upload but if the file name is "213134.jpg" but my code store database only name "213134" without file type. Can you please help me. I have used CI for this task
function postMessage(){
$config['upload_path'] = './uploads/inbox/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random;
$config['file_name'] =$pic;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$blog_msg ='Sorry, Picture uploaded.';
}
$u_id=$this->session->userdata('id');
$u_name=$this->session->userdata('u_fname');
$to=$this->uri->segment(3);
$date= $date=date('d-M-Y');
$message=$_POST["message"];
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$pic
);
}
$config['allowed_types'] = 'jpg|jpeg';//Change this
Method 01
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$this->input->post('pic'), //change this(in your HTML attribute name='' )
);
Method 02
or else you can try
$ext = pathinfo($pic, PATHINFO_EXTENSION); //Get the Extension
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random.$ext;//change this
$config['file_name'] =$pic;

why pdf upload fails on codeigniter 1.7.2?

i have same problem. other files doc, xls uploads fine. but pdf uploading gives error. “The filetype you are attempting to upload is not allowed.”
in config/mimnes.php i have:
'pdf' => array('application/pdf', 'application/x-download', 'application/download'),
in controllers i have:
function upload_file($type, $upload_type)
{
$this->load->library('upload');
//upload file
switch($upload_type){
case "image":
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpg|png|pdf';
$config['max_size'] = '3000';
$config['max_width'] = '3224';
$config['max_height'] = '1268';
break;
case "doc":
$config['upload_path'] = './uploads/pages/doc/';
$config['allowed_types'] = 'pdf|doc|docx|xls|ppt';
$config['max_size'] = '3000';
$config['encrypt_name'] = TRUE;
break;
}
foreach($_FILES as $key => $value)
{
if( ! empty($value['name']))
{
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($key))
{
$errors = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('flashError', $errors);
}
else
{
$this->page_model->process_file($type, $upload_type);
}
}
}
}
any help will be appreciable.
I had the same issue as well but figured out that some PDF files are sent as type application/octet-stream in FireFox. I don't know why. But try to open mimetype.php add it to the mimetype array of pdf file like this :
'pdf' => array('application/pdf', 'application/x-download', 'application/binary', 'application/octet-stream'),
Remove this line:
$this->load->library('upload');
Then add this line after end of switch block and before foreach block
$this->load->library('upload', $config);
You are loading the upload library and then setting config values, which has no effect.
Your modified function should look like this:
function upload_file($type, $upload_type)
{
//upload file
switch($upload_type){
case "image":
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpg|png|pdf';
$config['max_size'] = '3000';
$config['max_width'] = '3224';
$config['max_height'] = '1268';
break;
case "doc":
$config['upload_path'] = './uploads/pages/doc/';
$config['allowed_types'] = 'pdf|doc|docx|xls|ppt';
$config['max_size'] = '3000';
$config['encrypt_name'] = TRUE;
break;
}
$this->load->library('upload', $config);
foreach($_FILES as $key => $value)
{
if( ! empty($value['name']))
{
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($key))
{
$errors = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('flashError', $errors);
}
else
{
$this->page_model->process_file($type, $upload_type);
}
}
}
}
Are you using FireFox? I had the same problem, originated by FF. The mimetype reported is application/binary, so you can chage that in mimes.php
'pdf' => array('application/pdf', 'application/x-download', 'application/binary'),
that should solve the problem.

Resources