|
发表于 2008-7-29 14:38:48
|
显示全部楼层
需要自己扩展类。 我自己就随便写了个,多加了一个导入参数。判断参数是否为空即可
添加参数。 var $mofname =''; 并修改默认函数function set_filename($path, $filename)。
部分代码如下
PHP复制代码 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);
}
} 复制代码 |
评分
-
查看全部评分
|