Codeigniter 在上传时重命名文件 [英] Codeigniter Rename file on upload

查看:31
本文介绍了Codeigniter 在上传时重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在上传时添加时间作为图像名称的前缀以及原始名称,但我无法弄清楚.请帮我用以下代码在上传时为我的原始文件名添加前缀.

I'm trying to add time as the prefix of the image name along with the original name when uploading, But I couldn't figure it out. Please help me with the following code to add a prefix to my original file name when uploading.

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {


        $config['upload_path'] = 'Public/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';



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


        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }
}
?>

推荐答案

您可以使用 CI 原生选项加密文件名:

You can encrypt file name with use of CI native option:

$config['encrypt_name'] = TRUE;

您可以使用自己的代码来实现:

You can do it with your own code:

$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;

这篇关于Codeigniter 在上传时重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆