Codeigniter Multiple File Upload Messes File Extension

Codeigniter multiple file upload messes file extension

I solved this exact problem by amending the Upload.php files located in the library folder.

Comment out line 935:

$filename = $this->file_name;

Codeigniter multiple file upload messes file extension

I solved this exact problem by amending the Upload.php files located in the library folder.

Comment out line 935:

$filename = $this->file_name;

Codeigniter multiple file upload

You can upload any number of files

$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);

foreach ($_FILES as $key => $value) {

if (!empty($value['tmp_name']) && $value['size'] > 0) {

if (!$this->upload->do_upload($key)) {

$errors = $this->upload->display_errors();
flashMsg($errors);

} else {
// Code After Files Upload Success GOES HERE
}
}
}

And try using HTML like this:

<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />

Multiple File Upload with CodeIgniter - Offset Error

Update controller as follows..

public function dosya_yukle () {

$count = count($_FILES['dosya']['name']);
$files = $_FILES;
unset($_FILES);
for ($i=0; $i < $count; $i++) {

$_FILES['dosya']['name'] = $files['dosya']['name'][$i];
$_FILES['dosya']['type'] = $files['dosya']['type'][$i];
$_FILES['dosya']['tmp_name'] = $files['dosya']['tmp_name'][$i];
$_FILES['dosya']['error'] = $files['dosya']['error'][$i];
$_FILES['dosya']['size'] = $files['dosya']['size'][$i];

$config['upload_path'] = './assets/img';
$config['allowed_types'] = 'gif|jpg|png';

$this->load->library('upload', $config);

$this->upload->do_upload('dosya');

}
}

upload multiple image with different name using codeigniter

View Files

<div class="row">
<div class="col-md-4 col-md-offset-4">
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" enctype='multipart/form-data'>
<div class="form-group">
<label class="control-label">Mobile Image Front</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Back</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Side</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<input type="submit" value="upload"></form>
</div>
</div>

My controller:

<?php
class My_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
}
public function file_upload(){
$files = $_FILES;
$cpt = count($_FILES['mobile_img']['name']);
for($i=0; $i<$cpt; $i++)
{
$mobileView=(($i == 0)?"_front":(($i == 1)?"_back":"_side")); //Here i assign first image(i = 0) as _front, second image(i = 1) as _back,third image(i = 2) as _side
$_FILES['mobile_img']['name']= $files['mobile_img']['name'][$i].$mobileView.time();// time() for time in seconds becauze we may need if same name uploaded file name
$_FILES['mobile_img']['type']= $files['mobile_img']['type'][$i];
$_FILES['mobile_img']['tmp_name']= $files['mobile_img']['tmp_name'][$i];
$_FILES['mobile_img']['error']= $files['mobile_img']['error'][$i];
$_FILES['mobile_img']['size']= $files['mobile_img']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['mobile_img']['name'];
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->my_model->upload_image($fileName);
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
}
?>

My model Function:

<?php
class My_Model extends CI_Model {
public function upload_image($fileName)
{
if($filename!='' ){
$filename1 = explode(',',$filename);
foreach($filename1 as $file){
$file_data = array(
'name' => $file,
'datetime' => date('Y-m-d h:i:s')
);
$this->db->insert('uploaded_files', $file_data);
}
}
}
}
?>

Dividing Up CodeIgniter Multi-File DropZone Uploader Code into MVC?

Your createThumbnail function is a good example of something that should go either in a model or a library or a helper. A helper just contains functions, whereas a library or model are class based and therefore can access class properties and have constructors. I would lean towards making it either a library or a helper as it isn't coupled (thus reusable) to anything specific or other functions.

Generally you only want to return or throw Exceptions in any function that is not in a controller.

  1. This allows you to set your output in one file rather than having to
    search through your models, libraries, .etc. if you want to change
    your output down the road (for example if you plan on using AJAX).
  2. Also allows for code bases that aren't tightly coupled and can be
    easily ported to other installations that may be different.
  3. Good for readability.

Models

Anything database related should go in a model as models represent the data logic in or the actual data used by your application. You should really use Codeigniter's in-built database functions instead of initializing the database and running the query yourself. Your insert or update statements should move into a model related to the controller.

Controllers

Your controller is just used for loading models, and any variables the view may require. It also should be used to set the response, whether that be a flash message or json encoded. In many examples it is also used for form_validation or data checks before performing CRUD operations that exist within the related model.

tl;dr anything database related go in the model, anything reusable and not tightly coupled goes in a library, or if it has no specific relation to oop goes in a helper (like an array sorting algorithm), anything form/input/view/response related goes in a controller.


Roughly you can do something like this (code may not work out of the box; I use my own implementation that differs from the default dz behavior as I employing jquerys sortable as well... I also am unsure about the error handling as I do that using another technique):

// byte_helper.php

/**
* Gets max upload size from post_max_size and upload_max_filesize
* Returns whichever is smaller
*
* @return int
*/
function file_upload_max_size() {
$max_size = convert_to_bytes(ini_get('post_max_size'));
$upload_max = convert_to_bytes(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
return $max_size;
}

/**
* Converts KB (K) through GB (G) to bytes
*
* @param string $from
* @return int bytes
*/
function convert_to_bytes($from) {
$number = filter_var($from, FILTER_SANITIZE_NUMBER_INT);
$type = strtoupper(str_replace($number, '', $from));
switch ($type) {
case "KB":
case "K":
$number = $number * 1024;
break;
case "MB":
case "M":
$number = $number * pow(1024, 2);
break;
case "GB":
case "G":
$number = $number * pow(1024, 3);
break;
}
return fix_integer_overflow($number);
}

/**
* Fixes integer overflow
*
* @param int $size
* @return int
*/
function fix_integer_overflow($size) {
if ($size < 0) {
$size += 2.0 * (PHP_INT_MAX + 1);
}
return $size;
}

class Dropzone_controller extends CI_Controller {

public function upload() {
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$this->load->helper('byte');
$config['upload_path'] = ''; // upload directory realpath
$config['file_name'] = md5(time()); // I like to set my own filename
$config['overwrite'] = true;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = file_upload_max_size();
$config['file_ext_tolower'] = true;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
$this->dz_error($this->upload->display_errors('', '<br>'));
}
$data = $this->upload->data();
$full_path = realpath($data['full_path']); // CI uses weird slashes
// here you can use imagelib to resize if you want, I personally
// like to use verot's class upload for that
$this->load->model('dropzone_model');
if (!$this->dropzone_model->add_image($full_path)) {
@unlink($full_path); // remove orphaned image
$this->dz_error('Failed to add image to the database!');
}
$this->dz_success();
}

// https://stackoverflow.com/questions/27030652/how-to-display-error-message-of-jquery-dropzone
private function dz_error($error) {
$this->output
->set_header("HTTP/1.0 400 Bad Request")
->set_output($error)
->_display();
exit;
}

private function dz_success() {
// don't really need to do anything here?
}

}

class Dropzone_model extends CI_Model {

public function add_image($full_path) {
return $this->db->insert('sometable', array('image' => $full_path));
}

}


Related Topics



Leave a reply



Submit