【问题标题】:Unable to display capital letter file name in codeigniter无法在 codeigniter 中显示大写字母文件名
【发布时间】:2018-04-26 07:52:45
【问题描述】:

我已经在 CodeIgniter 中上传了多个文件,它在 localhost 中运行良好,但是当我上传实时服务器时,这些图像不会显示该名称是大写字母。我成功地将小写文件存储在数据库中,但在文件夹中(文件存储位置)我无法更改小写文件名(我认为这是问题所在)。 这是我的控制器

public function create() {
    // Check login
    if (!$this->session->userdata('logged_in')) {
        redirect('users/login');
    }

    $data['title']= $title = 'Create List';
    $path = 'assets/images/posts/';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('body', 'Body', 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('templates/header');
        $this->load->view('posts/create', $data);
        $this->load->view('templates/footer');
    } else {
        $this->load->library('upload');
        $files = $_FILES;
        $image = $files['images']['name'][0]; 
        $img = strtolower($image);
        $totimg = count($_FILES['images']['name']);

        if (!empty($_FILES['images']['name'][0])) {
            $post_image = $_FILES['images'];

            if ($this->upload_files($path, $title, $post_image) === FALSE) {
$data['error'] = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('file_size_exceeded', 'Your uploaded file size is too large');
                $post_image = 'noimage.jpg';
                redirect('posts');             
            }



        if (!isset($data['error'])) {
            $this->post_model->create_post($files,$totimg);
            $this->session->set_flashdata('post_created', 'Your post has been created');
            redirect('posts');
        } 
        }
    }
}

 private function upload_files($path, $title, $files)
{
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'jpg|gif|png',
        'overwrite'     => 1,
        'max_size'      => 2000,
        'remove_spaces' => TRUE
    );

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

    $images = array();

    foreach ($files['name'] as $key => $image) {

        $_FILES['images[]']['name']= $files['name'][$key];
        $_FILES['images[]']['type']= $files['type'][$key];
        $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images[]']['error']= $files['error'][$key];
        $_FILES['images[]']['size']= $files['size'][$key];

        $this->upload->initialize($config);

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            return false;
        }
    }

}

我的模型正在关注

public function create_post($post_image,$totimg){
                    $this->load->helper('inflector');
                    $slug = url_title($this->input->post('title'));
                    $image = implode(',',$post_image['images']['name']);
                    $file_name = underscore(strtolower($image));
                    $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'user_id' => $this->session->userdata('user_id'),
            'post_image' => $file_name
        );
                    return $this->db->insert('posts', $data);
    }

【问题讨论】:

  • 将文件名更改为数据库中的确切大小写并且应该解决它
  • Linux区分大小写,filename不等于FILENAME
  • 你试过了吗? $new_name = time().$_FILES["userfiles"]['name']; $config['file_name'] = $new_name;
  • @Mr.Blue 我收到了您的回复,但是如果上传同名文件然后在文件夹文件中更改为文件名(1),则会出现一个问题,但在显示中,它将显示前一个文件,如文件名。 . 我怎么能出来呢

标签: php codeigniter file-upload


【解决方案1】:

在控制器中,create() 函数 - 将 strtolower()underscore() 用于目标文件名,以便保存的文件名和数据库文件名相同。为了避免相同的文件名复制问题(其中 ci 在重复的文件名中添加副本号 - file1.jpg, file2.jpg ..),将 time() 添加到目标文件名以使其唯一。

紧随其后:

$files = $_FILES;

添加:

$this->load->helper('inflector');
for($i = 0;$i <count($files['images']['name']); $i++){
        $files['images']['name'][$i] = time()."_".underscore(strtolower($files['images']['name'][$i]));
}

替换:

$post_image = $_FILES['images'];

与:

$post_image = $files['images'];

创建()函数

更新代码:

public function create() {
    // Check login
    if (!$this->session->userdata('logged_in')) {
        redirect('users/login');
    }

    $data['title']= $title = 'Create List';
    $path = 'assets/images/posts/';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('body', 'Body', 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('templates/header');
        $this->load->view('posts/create', $data);
        $this->load->view('templates/footer');
    } else {
        $this->load->library('upload');
        $files = $_FILES;

        $this->load->helper('inflector');
        for($i = 0;$i <count($files['images']['name']); $i++){
            $files['images']['name'][$i] = time()."_".underscore(strtolower($files['images']['name'][$i]));
        }

        $image = $files['images']['name'][0]; 
        $img = strtolower($image);
        $totimg = count($_FILES['images']['name']);

        if (!empty($_FILES['images']['name'][0])) {
            $post_image = $files['images'];

            if ($this->upload_files($path, $title, $post_image) === FALSE) {
$data['error'] = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('file_size_exceeded', 'Your uploaded file size is too large');
                $post_image = 'noimage.jpg';
                redirect('posts');             
            }



        if (!isset($data['error'])) {
            $this->post_model->create_post($files,$totimg);
            $this->session->set_flashdata('post_created', 'Your post has been created');
            redirect('posts');
        } 
        }
    }
}

【讨论】:

  • 好的..现在我将在文件名的开头添加时间..所以它会是唯一的名称
  • 更新了我的答案。
  • 我之前也做过同样的事情,但你帮了我很多。谢谢
猜你喜欢
  • 1970-01-01
  • 2022-06-30
  • 2013-12-13
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多