gpt4 book ai didi

codeigniter - 在 CodeIgniter 中重命名上传的文件

转载 作者:行者123 更新时间:2023-12-03 17:41:10 25 4
gpt4 key购买 nike

使用 CodeIgniter,我试图通过删除任何空格并大写后续单词来将上传文件的名称修改为驼峰式命名法。

我很确定我可以使用 move_uploaded_file 的第二个参数重命名文件,但我什至不知道在哪里寻找如何将名称修改为camelCase。

提前致谢!
乔恩

最佳答案

查看 CI 的上传库:

http://www.codeigniter.com/user_guide/libraries/file_uploading.html

我们先来看看如何在不更改文件名的情况下进行简单的文件上传:

$config['upload_path']   = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|gif|png';

$this->upload->initialize($config);

if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
}

就这么简单,而且效果很好。

现在,让我们来看看问题的实质。首先,我们需要从 $_FILES 数组中获取文件名:
$file_name = $_FILES['file_var_name']['name'];

然后我们可以用 _ 分割字符串像这样的分隔符:
$file_name_pieces = split('_', $file_name);

然后我们必须遍历列表并创建一个新字符串,其中除了第一个位置之外的所有字符串都具有大写字母:
$new_file_name = '';
$count = 1;

foreach($file_name_pieces as $piece)
{
if ($count !== 1)
{
$piece = ucfirst($piece);
}

$new_file_name .= $piece;
$count++;
}

现在我们有了新的文件名,我们可以重新审视我们上面所做的。基本上,除了添加这个 $config 参数之外,你做的一切都是一样的:
$config['file_name'] = $new_file_name;

那应该这样做!默认情况下,CI 具有 overwrite $config 参数设置为 FALSE ,所以如果有任何冲突,它会在您的文件名末尾附加一个数字。有关参数的完整列表,请参阅本文顶部的链接。

关于codeigniter - 在 CodeIgniter 中重命名上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4073752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com