如何在codeigniter中上传重命名的图像

时间:2016-06-02 12:13:31

标签: image codeigniter upload

我想在codeigniter中上传图片。但是如何上传重命名的图片,请参阅以下代码。

$filename = $_FILES['filename']['name']."_".date("Y-m-d")."_".date("H:i:s");

public function index()
{  
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('description', 'Description', 'required');
    $this->form_validation->set_rules('author', 'Author', 'required');

    $filename = $_FILES['filename']['name']."_".date("Y-m-d")."_".date("H:i:s");

      $this->do_upload();

      $this->load->model('News_model');
      $this->News_model->insert_news($filename);

      $this->load->view('news/success', $data);     
}

1 个答案:

答案 0 :(得分:0)

这就是我要做的事情:

$folder = "./Path/To/Uploaded/files"
$config['upload_path'] = $folder;

// We initiate the CI upload library
$this->load->library('upload', $config);

if (!$this->upload->do_upload('file')) {
    $error = array('error' => $this->upload->display_errors());
} else {
    // Upload success, we get back the image info
    $data = array('upload_data' => $this->upload->data());

    // we rename the file
    $filename = $data['upload_data']['raw_name'] . "_".date("Y-m-d") . "_" . date("H:i:s") . $data['upload_data']['file_ext'];
    rename($data['upload_data']['full_path'], $folder . $filename);
}

请参阅Codeigniter文档中的详细信息:https://www.codeigniter.com/user_guide/libraries/file_uploading.html