如果该值已经存在于数据库(CodeIgniter PHP)中,则将计数添加到图像文件名


  1. 我尝试通过用户也将图像数据发送到数据库表的用户上传(到文件夹目录'gambar')。

  2. dinamcy,在下面的代码上,如果图像文件名已经存在,它将以次数为最后一个字符(例如: my-path.jpg ,my-path.png, my-path1.jpg )。

  3. 但是,使用相同的代码,它不会以与上述相同的路径和延伸的图像进行计数器。

  4. 我使用的是Codeigniter 3.1.2,带有PHP 5.3

问题:如何确保此代码还插入与点No.2相同的路径(唯一添加counter如果 file-name.extension.extension 已经存在)


控制器> proterne_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);
        }

    }
}

视图> prouty_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>

我的目标:我不会回电图像URL(ex: mysite.com/gambar/image-path.jpg mysite.com/gambar/image-path1。jpg ),由用户在我的wysiwyg上上传,而不会通过提供从此表生成的JSON数据来刷新页面。

我有一个想法如何做到这一点...由于您将用户上传的所有文件名保存到数据库,因此那么您可以检查文件名是否已经存在然后,如果存在,您可以获取最后一个计数器,然后将其添加1,然后将其与当前文件名

concat

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
    }
}

最新更新