I try to call back images path uploaded (to folder directory 'gambar') by user with method send image data(s) to database table too.
Dinamically, with code below, if image file name already exist, it will save with count as last character (ex: my-path.jpg, my-path.png, my-path1.jpg).
But with the same code, it not generate counter of image with same path and extension dinamically as above.
I'm using Codeigniter 3.1.2 with PHP 5.3
QUESTION: how to make sure this code also insert path same as point no.2 (unique by adding counter if file-name.extension already exist)
controllers > multiple_upload.php
<?php
class multiple_upload extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index() {
//load file upload form
$this->load->view('multiple_upload_view');
}
function upload() {
// set upload preferences
$config['upload_path'] = './gambar/';
$config['allowed_types'] = 'png|jpg|gif|jpeg|JPG|JPEG|GIF|PNG';
$config['max_size'] = '150';
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for($i=0; $i<count($_FILES['usr_files']['name']); $i++) {
$_FILES['userfile']['name']= $_FILES['usr_files']['name'][$i];
$_FILES['userfile']['type']= $_FILES['usr_files']['type'][$i];
$_FILES['userfile']['tmp_name']= $_FILES['usr_files']['tmp_name'][$i];
$_FILES['userfile']['error']= $_FILES['usr_files']['error'][$i];
$_FILES['userfile']['size']= $_FILES['usr_files']['size'][$i];
if (!$this->upload->do_upload()) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('multiple_upload_view', $upload_error);
break;
} else {
$filename = $_FILES['usr_files']['name'][$i];
$haha = $_FILES['usr_files']['tmp_name'][$i];
//-- CODE SEND TO DATABASE START
$kepalabana = array(
'img_path' => $filename,
'img_name' => 'lol',
'login_session_id' => 'wtf',
'user_id' => 'wth'
);
$this->db->insert('img_tbl', $kepalabana); // my table name is img_tbl
//-- CODE SEND TO DATABASE END
}
}
// success
if ($upload_error == NULL) {
$data['success_msg'] = '<div class="alert alert-success text-center">Done, sila upload gambar lain..</div>';
$this->load->view('multiple_upload_view', $data);
}
}
}
views > multiple_upload_view.php
<div class="container">
<div class="row">
<div class="col-xs-8 col-xs-offset-2 well">
<?php echo form_open_multipart('multiple_upload/upload');?>
<legend>Select Files to Upload:</legend>
<div class="form-group">
<input name="usr_files[]" type="file" multiple="" />
<span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>
</div>
<div class="form-group">
<input type="submit" value="Upload" class="btn btn-primary btn-block"/>
</div>
<?php echo form_close(); ?>
<?php if (isset($success_msg)) { echo $success_msg; } ?>
</div>
</div>
</div>
My goal: I wan't to call back image URL (ex: mysite.com/gambar/image-path.jpg or mysite.com/gambar/image-path1.jpg), that uploaded by user at my WYSIWYG without refreshing the page by providing json data that generate from this table.
i have an idea how to do this ... since you save to database for all the file name that the user has upload, then you can check whether the filename already exists or not and then if exists, you can get the last counter and then add it with 1 and concat it with the current file name
ex :
for($i=0; $i<count($_FILES['usr_files']['name']); $i++) {
/*
//you should use id to get max id
$strsql = "select max(id) as id from img_tbl where img_path like '".$_FILES['usr_files']['name'][$i]."%'";
$checkcounter = $this->db->query($strsql);
$newcounter = '';
if (count($checkcounter) > 0) {
$lastcounter = $this->db->query("select img_path from img_tbl where id = ".$checkcounter[0]['id']);
$newcounter = getcounter from lastcounter //=> you can use subtring and length to get the last counter from filename
$newcounter = $newcounter + 1
}
*/
$newfilename = $_FILES['usr_files']['name'][$i] . '-' . $newcounter; //=> this is new filename contain the counter
$_FILES['userfile']['name']= $_FILES['usr_files']['name'][$i];
$_FILES['userfile']['type']= $_FILES['usr_files']['type'][$i];
$_FILES['userfile']['tmp_name']= $_FILES['usr_files']['tmp_name'][$i];
$_FILES['userfile']['error']= $_FILES['usr_files']['error'][$i];
$_FILES['userfile']['size']= $_FILES['usr_files']['size'][$i];
if (!$this->upload->do_upload()) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('multiple_upload_view', $upload_error);
break;
} else {
$filename = $_FILES['usr_files']['name'][$i];
$haha = $_FILES['usr_files']['tmp_name'][$i];
//-- CODE SEND TO DATABASE START
$kepalabana = array(
'img_path' => $filename,
'img_name' => 'lol',
'login_session_id' => 'wtf',
'user_id' => 'wth'
);
$this->db->insert('img_tbl', $kepalabana); // my table name is img_tbl
//-- CODE SEND TO DATABASE END
}
}