我尝试回拨用户上传的图像路径(到文件夹目录' gambar'),方法也将图像数据发送到数据库表。
在下面的代码中,如果图像文件名已经存在,它将以计数作为最后一个字符保存(例如: my-path.jpg ,my-path.png, MY-path1.jpg )。
但是使用相同的代码,它不会像上面那样生成具有相同路径和扩展名的图像计数器。
我使用Codeigniter 3.1.2和PHP 5.3
问题:如何确保此代码也插入与第2点相同的路径(如果 file-name.extension 已经存在,则添加计数器是唯一的)
控制器> 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);
}
}
}
观看&gt; 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>
我的目标:我不打算回拨图片网址(例如: mysite.com/gambar/image-path.jpg 或 mysite.com/gambar/image- path1.jpg ),由用户在我的WYSIWYG上传,而不通过提供从该表生成的json数据来刷新页面。
答案 0 :(得分:0)
我知道如何做到这一点...... 因为您为用户上传的所有文件名保存到数据库, 然后你可以检查文件名是否已经存在 然后如果存在,你可以得到最后一个计数器然后用1添加它并用当前文件名连接它
前:
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
}
}