我正在尝试在上传时添加时间作为图像名称的前缀以及原始名称,但我无法弄明白。请帮我使用以下代码在上传时为我的原始文件名添加前缀。
<?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);
}
}
}
?>
答案 0 :(得分:77)
您可以使用CI本机选项加密文件名:
$config['encrypt_name'] = TRUE;
或强>
您可以使用自己的代码执行此操作:
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
答案 1 :(得分:7)
由于某些原因,对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 ');
文件名全部为“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())...
我认为这是因为第二次调用它时这行不起作用。它不会再次设置配置
$this->load->library('upload', $config);
=============================================== =========================== 解决连续do_upload函数调用期间遇到的问题:
// re-initialize upload library
$this->upload->initialize($config);
答案 2 :(得分:2)
对于你正在寻找的东西,这对我有用:
$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION);
答案 3 :(得分:1)
使用do_upload()函数时,其重命名文件名(如果已存在)并上传文件
系统->库-> Upload.php
do_upload()的默认函数返回true或false 替换
返回$ this-> upload_path。$ this-> file_name;
和另一个文件
<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;
}
}
?>
答案 4 :(得分:0)
$config['file_name'] = $new_name;
Just add it align with the config codes.
答案 5 :(得分:0)
这应该是在上传之后。
在此处理您想要的文件名:
$a=$this->upload->data();
rename($a['full_path'],$a['file_path'].'file_name_you_want');
答案 6 :(得分:0)
尝试一下:
$config['file_name'] = "yourCustomFileName";
答案 7 :(得分:0)
/* 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);
答案 8 :(得分:0)
如果您的aws sts get-caller-identity
使用以下代码:$config
文件名将被强制使用加密名称重命名,只需删除代码即可。
答案 9 :(得分:0)
解决方案 1: 上传具有新的自定义和特定名称的文件(注意:此答案适用于您提交记录的唯一名称时,例如学生姓名是记录中的唯一值那么你将使用 $_POST['student_name']
)
$config['file_name'] = str_replace(' ', '_', $_POST['anyUniqueValue']);
//any Other unique value that you are using to submit with this file
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
解决方案 2:使用新的唯一名称上传文件
$config['file_name'] = time();
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
解决方案 3: 使用 codeigniter 的默认选项上传具有新唯一名称的文件
$config['encrypt_name'] = TRUE;