php Codeigniter 在上传时重命名文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21811773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter Rename file on upload
提问by Dilukshan Mahendra
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);
}
}
}
?>
回答by Kumar V
You can encrypt file name with use of CI native option:
您可以使用 CI 本机选项加密文件名:
$config['encrypt_name'] = TRUE;
OR
或者
You can do it with your own code:
你可以用你自己的代码来做到这一点:
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
回答by Carmela
For some reasons, consecutive calls to the do_upload function doesn't work. It sticks to the first filename set by the first function call
由于某些原因,对 do_upload 函数的连续调用不起作用。它坚持第一个函数调用设置的第一个文件名
$small_photo_url = $this->upload_photo('picture_small', $this->next_id.'_small ');
$medium_photo_url = $this->upload_photo('picture_medium', $this->next_id.'_medium');
$large_photo_url = $this->upload_photo('picture_large', $this->next_id.'_large ');
The filenames will all be "00001_small", "00001_small1", "00001_small2" given the following configurations
给定以下配置,文件名都将为“00001_small”、“00001_small1”、“00001_small2”
function upload_photo($field_name, $filename)
{
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $filename;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())...
I think it's because this line doesn't work the second time you call it. It does not set the configurations again
我认为这是因为这条线路在您第二次调用它时不起作用。它不会再次设置配置
$this->load->library('upload', $config);
========================================================================== Solution to the problem faced during consecutive do_upload function calls:
================================================== ======================== 连续调用do_upload函数时遇到的问题的解决方法:
// re-initialize upload library
$this->upload->initialize($config);
回答by Heshan Rajapaksha
For what you are exactly looking for, this worked for me:
对于您正在寻找的内容,这对我有用:
$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION);
回答by Parveen Chauhan
/* Conf */
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '';
$config['max_width'] = '';
$config['max_height'] = '';
$config['file_ext_tolower'] = TRUE;
/* Load the upload library */
$this->load->library('upload', $config);
回答by pruthwiraj.kadam
when you use do_upload() function its rename file name if already exists and upload file
当您使用 do_upload() 函数时,它的重命名文件名(如果已经存在)并上传文件
System->libraries->Upload.php
系统->库->Upload.php
default function of do_upload() return true or false replace
do_upload() 的默认函数返回 true 或 false 替换
return $this->upload_path.$this->file_name;
返回 $this->upload_path.$this->file_name;
and in another file
并在另一个文件中
<input type='file' name='userfile'/>
<?php
$upload_file_name = $this->upload->do_upload('userfile');
$finalname;
if (!$upload_file_name) {
$finalname = 'default.png';
} else {
$split_data_upload_file_name = explode("/", $upload_file_name);
foreach ($split_data_upload_file_name as $i => $value) {
$finalname = $value;
}
}
?>
回答by Rafiqul Islam
Try This:
尝试这个:
$config['file_name'] = "yourCustomFileName";
回答by Jesus Erwin Suarez
$config['file_name'] = $new_name;
Just add it align with the config codes.
只需添加它与配置代码对齐。
回答by user2955330
This should be after the upload.
这应该是在上传之后。
Process the filename you want here:
在此处处理您想要的文件名:
$a=$this->upload->data();
rename($a['full_path'],$a['file_path'].'file_name_you_want');

