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
Related
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);
}
Here is my controller function, please help me to create the thumbs of both images. Only the images are uploaded to the folder. i created a function named resize to create the thumbs. that's also given in the controller.
public function add() {
$this->load->helper(array('form', 'url'));
$this->load->helper('file');
$this->load->library('form_validation');
$this->form_validation->set_rules('txtPrdname', 'Product Name', 'trim|required|htmlspecialchars');
$this->form_validation->set_rules('sbPrdcategory', 'Product Category', 'trim|required|htmlspecialchars');
$this->form_validation->set_rules('sbPrduser', 'Managing User', 'trim|required|htmlspecialchars');
$this->form_validation->set_rules('txtPrdprofile', 'Product Profile', 'trim|required|htmlspecialchars');
if ($this->form_validation->run() == FALSE) {
$data_view["error"] = "";
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
$this->load->view('moderator/b2bproduct_add', $data_view);
$this->load->view('moderator/templates/footer');
} else {
// Image uploading codes
$config['upload_path'] = 'assets/images/b2bproduct';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdimage']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdimage']['name'];
}
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('filePrdimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->load->library('upload');
$this->upload->initialize($config);
$this->resize($dat['upload_data']['full_path'], 'assets/images/b2bproduct/thump/' . $dat['upload_data']['file_name'], 180, 400);
}
if (empty($dat['upload_data']['file_name'])) {
$prdimage = '';
} else {
$prdimage = $dat['upload_data']['file_name'];
}
// End Image uploading Codes
// Logo uploading codes
$config['upload_path'] = 'assets/images/b2blogo';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdlogo']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdlogo']['name'];
}
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('filePrdlogo')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat1 = array('upload_data' => $this->upload->data());
$this->load->library("upload",$config);
$this->resize($dat1['upload_data']['full_path'], 'assets/images/b2blogo/thump/' . $dat1['upload_data']['file_name'], 180, 400);
}
if (empty($dat1['upload_data']['file_name'])) {
$prdlogo = '';
} else {
$prdlogo = $dat1['upload_data']['file_name'];
}
// End Logo uploading Codes
$data = array(
'prd_name' => $this->input->post('txtPrdname'),
'prd_category' => $this->input->post('sbPrdcategory'),
'prd_user' => $this->input->post('sbPrduser'),
'prd_profile' => $this->input->post('txtPrdprofile'),
'prd_oem' => $this->input->post('rbtnPrdoem'),
'prd_protype' => $this->input->post('rbtnPrdprotype'),
'prd_image' => $prdimage,
'prd_ranktype' => $this->input->post('sbPrdranktype'),
'prd_points' => $this->input->post('txtPrdpoints'),
'prd_extrakey' => $this->input->post('txtPrdextrakey'),
'prd_dated' => time(),
'prd_ipadd' => $_SERVER['REMOTE_ADDR']
);
$result_id = $this->b2bproduct_model->add($data);
if ($result_id) {
redirect(base_url() . 'moderator/b2bproduct/view/' . $result_id, 'refresh');
} else {
$data_view["error"] = "Data can't insert due to database error";
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
$this->load->view('moderator/b2bproduct_add', $data_view);
$this->load->view('moderator/templates/footer');
}
}
}
Resize function
public function resize($source, $destination, $width, $height) {
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $destination;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
First of all you are loading library two times in your function add please load it one time probably at the top of function.
in resize use $this->image_lib->initialize($config) as below
public function resize($source, $destination, $width, $height) {
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $destination;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
Is it possible that the folders for your thumbs (assets/images/b2bproduct/thump/ and assets/images/b2blogo/thump/) do not exist?
It is very likely the reason to be a simple spelling mistake like thump instead of thumb.
EDIT:
You really don't have to load the upload and image_lib libraries so many times. Do it once at the beginning. After that you can use $this->upload->initialize($config); or $this->image_lib->initialize($config); to all these places where now you are trying to re-load the libraries.
To make your code works you should at least add $this->image_lib->initialize($config); before $this->image_lib->resize(); in your resize function.
on this way i can upload image and pdf..
but i want to download the pdf or image from my view...please someone give me the code of downloading pdf or image from view..give me the full code
public function save_about_1() {
$about_1_image_info = $this->w_model->select_about_1_image();
$image_path = explode(base_url(), $about_1_info->about_1_link, 2);
unlink($image_path[1]);
$this->sa_model->delete_about_1($about_1_info->about_1_id);
$data = array();
/* Uplod start */
$config['upload_path'] = 'images/about_1/';
$config['allowed_types'] = 'gif|jpg|png|pdf|doc|xml';
$config[ 'overwrite'] = TRUE;
$config['max_size'] = '10000kb';
$config['max_width'] = '100240';
$config['max_height'] = '76800';
$error = array();
$fdata = array();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('about_1_link')) {
$error = $this->upload->display_errors();
$edata = array();
$edata['error_message'] = $error;
$this->session->set_userdata($edata);
redirect('super_admin/about_1');
} else {
$fdata = $this->upload->data();
$data['about_1_link'] = base_url() . $config['upload_path'] . $fdata['file_name'];
$this->sa_model->save_about_1_info($data);
$sdata = array();
$sdata['message'] = "Saved Image Successfully";
$this->session->set_userdata($sdata);
redirect('super_admin/about_1');
}
}
You can use the CI download helper for this.
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';
force_download($name, $data);
From the CI User Guide.
https://ellislab.com/codeigniter/user-guide/helpers/download_helper.html
I am Trying to Upload two file with from Same form with Different File name.
The First File is always Uploading But the Other one never Uploading.
code for Model is:
public function add_imgup()
{
$this->uppdf();
$this->upphoto();
}
public function upphoto()
{
$upload = array();
$current_timestamp = time();
$image_name = $current_timestamp.'_image';
$config['upload_path'] = $this->config->item('file_upload_absolute_path')."pdf/";
$config['allowed_types'] = 'pdf|PDF|DOC|doc|docx';
$config['overwrite'] = true;
$config['file_name'] = $image_name;
$this->load->library('upload',$config);
$file_image = $this->upload->do_upload('ccv');
$image = array('upload_data' => $this->upload->data());
}
public function uppdf()
{
$upload = array();
$current_timestamp = time();
$image_name = $current_timestamp.'_image';
$config['upload_path'] = $this->config->item('file_upload_absolute_path')."photo/";
$config['allowed_types'] = 'jpg|jpeg|png|gif|bmp';
$config['overwrite'] = true;
$config['file_name'] = $image_name;
$this->load->library('upload',$config);
$file_image = $this->upload->do_upload('iimg');
$image = array('upload_data' => $this->upload->data());
}
Check the print_r($_FILE) after submitting the form to confirm the you submitted correct form, also check your form is enctype="multipart/form-data"
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.