【问题标题】:How to upload renamed image in codeigniter如何在codeigniter中上传重命名的图像
【发布时间】:2016-10-02 04:10:10
【问题描述】:

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

【问题讨论】:

  • 您找到解决问题的方法了吗?

标签: image codeigniter upload


【解决方案1】:

这就是我正在做的事情:

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 2011-10-01
    • 2012-10-06
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多