【问题标题】:Cannot set new image name in codeigniter无法在 codeigniter 中设置新的图像名称
【发布时间】:2021-02-15 04:11:37
【问题描述】:

我在 codeigniter 中上传了多个图像,但是当我使用此代码设置新名称时 $_FILES['attachment']['name']= $filename;

我明白了

您尝试上传的文件类型不允许。

请提出解决此问题的解决方案。

public function do_upload()
{
    if (($_SERVER['REQUEST_METHOD']) == "POST") {
        $count = count($_FILES['attach_file']['name']);
        $files = $_FILES;
        for($i=0; $i<$count; $i++){
        $filename = $_FILES['attach_file']['name'][$i];
        $filename = strstr($filename, '.', true);
        $email    = $this->session->userdata('email');
        $filename = strstr($email, '@', true)."_".$filename;
        $filename = strtolower($filename);

        $_FILES['attachment']['name']= $filename;
        $_FILES['attachment']['type']= $_FILES['attach_file']['type'][$i];
        $_FILES['attachment']['tmp_name']= $_FILES['attach_file']['tmp_name'][$i];
        $_FILES['attachment']['error']= $_FILES['attach_file']['error'][$i];
        $_FILES['attachment']['size']= $_FILES['attach_file']['size'][$i];    
        $config['upload_path']   = FCPATH .'./assets/attachments/new/';
        $config['allowed_types'] = 'pdf|doc|docx|bmp|gif|jpg|jpeg|jpe|png';
        $config['max_size']      = 0;
        $config['max_width']     = 0;
        $config['max_height']    = 0;
        $config['encrypt_name']  = true; 
        $config['file_ext_tolower'] = true; 
        $config['overwrite']     = false;
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('attachment')) {
            $data['exception'] = $this->upload->display_errors();
            $data['status'] = false;
            echo json_encode($data);
        } else {
            $upload =  $this->upload->data();
            $data['message'] = 'upload_successfully';
            $data['filepath'] = './assets/attachments/'.$upload['file_name'];
            $data['status'] = true;
            echo json_encode($data);
        }
    }  
    }
}

【问题讨论】:

  • 您上传的是哪种文件类型??
  • @KUMAR:我正在上传 jpeg 类型的文件
  • 请删除 $config['max_size'] = 0; $config['max_width'] = 0; $config['max_height'] = 0; 并告诉我。
  • 您使用的是哪个版本:CI 2.x 还是 3.x?
  • @KUMAR: 删除代码但得到相同的错误

标签: image codeigniter model-view-controller codeigniter-3 codeigniter-2


【解决方案1】:

注意:-您将再次分配相同的变量名$filename

请更改:-

$filename = $_FILES['attach_file']['name'][$i];
$filename = strstr($filename, '.', true);
$email    = $this->session->userdata('email');
$filename = strstr($email, '@', true)."_".$filename;
$filename = strtolower($filename);

$_FILES['attachment']['name']= $filename;

到:-

$filename = $_FILES['attach_file']['name'][$i];
$filename1 = strstr($filename, '.', true);
$email    = $this->session->userdata('email');
$filename2 = strstr($email, '@', true)."_".$filename1;
$filename3 = strtolower($filename2);  

【讨论】:

  • 尝试了上面的代码,但是当我拉 $config["allowed_types"] ="*";然后上传一个没有任何扩展名的普通文件
  • @user3653474 上传的文件夹中的文件名是否有变化??
  • @KUMAR- 不,它不会更改图像文件名
  • @user3653474 您想在插入前同时更改uploaded image nametable insert name??
  • @user3653474 只是回显$filename3 = strtolower($filename2); 它返回的文件名相同还是不同??
猜你喜欢
  • 2012-10-17
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2011-09-18
  • 2016-04-13
  • 2017-10-24
  • 1970-01-01
相关资源
最近更新 更多