无法在codeigniter中显示大写字母文件名

时间:2018-04-26 07:52:45

标签: php codeigniter file-upload

我已经在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);
    }

1 个答案:

答案 0 :(得分:0)

在控制器中,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');
        } 
        }
    }
}