[英]Codeigniter Rename file on upload
我试图在上传时添加时间作为图像名称的前缀以及原始名称,但我无法弄清楚。 请帮助我使用以下代码在上传时为我的原始文件名添加前缀。
<?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 本机选项加密文件名:
$config['encrypt_name'] = TRUE;
或者
你可以用你自己的代码来做到这一点:
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
由于某些原因,对 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);
对于您正在寻找的内容,这对我有用:
$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION);
/* 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);
当您使用 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;
}
}
?>
$config['file_name'] = $new_name;
只需添加它与配置代码对齐。
这应该是在上传之后。
在此处处理您想要的文件名:
$a=$this->upload->data();
rename($a['full_path'],$a['file_path'].'file_name_you_want');
尝试这个:
$config['file_name'] = "yourCustomFileName";
如果您的$config
使用以下代码: $config['encrypt_name'] = TRUE;
您的文件名将被强制重命名为加密名称,只需删除代码即可。
解决方案 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.