gz123 发表于 2008-5-23 10:12:12

在上传文件时,怎么能给上传的文件自己命名?

没有找到怎么传递自己给文件命名的方法,现在如果上传同名文件,例如aa.jpg,CI会自己更改文件名为aa1.jpg,如果设置 $config['encrypt_name'] = TRUE ;CI会随机命名文件,应该可以自己命名文件的吧,那位仁兄指教一下。

gz123 发表于 2008-5-23 11:31:26

怎么没人回答呢:(

cc63764977 发表于 2008-6-4 15:03:03

我也不知道,希望懂的人能教小弟一下,不胜感激

kkito 发表于 2008-6-4 20:19:46

aa.jpg --> aa1.jpg
我好像没有这种情况发生
该啥文件名就是啥文件名

dongjian1984 发表于 2008-7-29 14:38:48

需要自己扩展类。我自己就随便写了个,多加了一个导入参数。判断参数是否为空即可
添加参数。        var $mofname      =''; 并修改默认函数function set_filename($path, $filename)。
部分代码如下
class CI_Upload {
       
        var $max_size                = 0;
        var $max_width                = 0;
        var $max_height                = 0;
        var $allowed_types        = "";
        var $file_temp                = "";
        var $file_name                = "";
        var $orig_name                = "";
        var $file_type                = "";
        var $file_size                = "";
        var $file_ext                = "";
        var $upload_path        = "";
        var $overwrite                = FALSE;
        var $encrypt_name        = FALSE;
        var $mofname      ='';
        var $is_image                = FALSE;
        var $image_width        = '';
        var $image_height        = '';
        var $image_type                = '';
        var $image_size_str        = '';
        var $error_msg                = array();
        var $mimes                        = array();
        var $remove_spaces        = TRUE;
        var $xss_clean                = FALSE;
        var $temp_prefix        = "temp_file_";

        function initialize($config = array())
        {
                $defaults = array(
                                                        'max_size'                        => 0,
                                                        'max_width'                        => 0,
                                                        'max_height'                => 0,
                                                        'allowed_types'                => "",
                                                        'file_temp'                        => "",
                                                        'file_name'                        => "",
                                                        'orig_name'                        => "",
                                                        'file_type'                        => "",
                                                        'file_size'                        => "",
                                                        'file_ext'                        => "",
                                                        'upload_path'                => "",
                                                        'overwrite'                        => FALSE,
                                                        'encrypt_name'                => FALSE,
                                                        'mofname'         => "",
                                                        'is_image'                        => FALSE,
                                                        'image_width'                => '',
                                                        'image_height'                => '',
                                                        'image_type'                => '',
                                                        'image_size_str'        => '',
                                                        'error_msg'                        => array(),
                                                        'mimes'                                => array(),
                                                        'remove_spaces'                => TRUE,
                                                        'xss_clean'                        => FALSE,
                                                        'temp_prefix'                => "temp_file_"
                                                );       
       
       
                foreach ($defaults as $key => $val)
                {
                        if (isset($config[$key]))
                        {
                                $method = 'set_'.$key;
                                if (method_exists($this, $method))
                                {
                                        $this->$method($config[$key]);
                                }
                                else
                                {
                                        $this->$key = $config[$key];
                                }                       
                        }
                        else
                        {
                                $this->$key = $val;
                        }
                }
        }
       
        // --------------------------------------------------------------------
               
        /**
       * Set the file name
       *
       * This function takes a filename/path as input and looks for the
       * existence of a file with the same name. If found, it will append a
       * number to the end of the filename to avoid overwriting a pre-existing file.
       *
       * @access        public
       * @param        string
       * @param        string
       * @return        string
       */       
        function set_filename($path, $filename)
        {
                if ($this->encrypt_name == TRUE)
                {               
                        mt_srand();
                        $filename = md5(uniqid(mt_rand())).$this->file_ext;                        
                }
                if ($this->mofname<>'')
                {               
                        $filename =$this->mofname.$this->file_ext;                        
                }       
                if ( ! file_exists($path.$filename))
                {
                        return $filename;
                }
       
                $filename = str_replace($this->file_ext, '', $filename);
               
                $new_filename = '';
                for ($i = 1; $i < 100; $i++)
                {                       
                        if ( ! file_exists($path.$filename.$i.$this->file_ext))
                        {
                                $new_filename = $filename.$i.$this->file_ext;
                                break;
                        }
                }

                if ($new_filename == '')
                {
                        $this->set_error('upload_bad_filename');
                        return FALSE;
                }
                else
                {
                        return $new_filename;
                }
        }
       
        // --------------------------------------------------------------------
//。。。。略。
//调用时,加个参数即可重新命名
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']= '1024';
$config['max_height']= '768';
$config['remove_spaces']= true;
$config['mofname']='newname1111111';
$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);
}
}
页: [1]
查看完整版本: 在上传文件时,怎么能给上传的文件自己命名?