why pdf upload fails on codeigniter 1.7.2? - codeigniter

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.

Related

Multiple image uploading error in Codeigniter

I am trying to upload the multiple images through my form. I have taken care of many expect like form_open_multipart, calling a name as Array in upload input.
I have no issue with the images upload. I can upload multiple images successfully, but the problem comes when I select any text or non-image file with the images.
I am checking scenarios that if user mixes it up (accidently select any other file with images) images with text file then some files are getting uploaded and then I get the error of the wrong type of file.
In Ideal case, it may not allow uploading the file if any one file does not have the correct file type.
so can any one help me over that how can I check before file upload that all files are allowed type or not?
I tried many examples but get the same problem In all.
There is no problem if i upload the multiple images. It's work fine.
<input type="file" name="pic_url[]" multiple accept="image/*" />
And this below is my controller
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
}
First problem is primarily down to how you handle your errors.
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
The above code will simply halt the rest of the for loop and redirect on the first error.
A simple solution would be to change that to log failures and continue processing:
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
$upload_error[$i] = array('error' => $this->upload->display_errors());
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Alternatively, you would need to look at manually validating files based on their file extension.
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
foreach ($_FILES['pic_url'] as $key => $file) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if (strpos($config['allowed_types'], $ext) === false) {
$upload_error[$key] => array('error' => sprintf('<p>Invalid file extension (%s)</p>', $ext));
continue;
}
$_FILES['userfile'] = $file;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$upload_error[$key] = array('error' => $this->upload->display_errors());
} else {
// do something with $this->upload->data();
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Hope that helps

image not storing in folder using 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);
}

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;

Resources