使用CodeIgniter,我试图通过删除任何空格和大写后续单词来修改上传文件的名称为camelCase.
我很确定我可以使用move_uploaded_file的第二个参数重命名该文件,但我甚至不知道在哪里查找如何将名称修改为camelCase.
提前致谢!乔恩
tre*_*ace 14
查看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();
}
Run Code Online (Sandbox Code Playgroud)
这很简单,效果很好.
现在,让我们来看看问题的关键.首先,我们需要从$ _FILES数组中获取文件名:
$file_name = $_FILES['file_var_name']['name'];
Run Code Online (Sandbox Code Playgroud)
然后我们可以用这样的_分隔符拆分字符串:
$file_name_pieces = split('_', $file_name);
Run Code Online (Sandbox Code Playgroud)
然后我们必须迭代列表并创建一个新的字符串,除了第一个点之外的所有字符串都有大写字母:
$new_file_name = '';
$count = 1;
foreach($file_name_pieces as $piece)
{
if ($count !== 1)
{
$piece = ucfirst($piece);
}
$new_file_name .= $piece;
$count++;
}
Run Code Online (Sandbox Code Playgroud)
现在我们有了新的文件名,我们可以重新审视上面的内容.基本上,除了添加这个$ config参数之外,你做的一切都是一样的:
$config['file_name'] = $new_file_name;
Run Code Online (Sandbox Code Playgroud)
这应该做到!默认情况下,CI将overwrite$ config参数设置为FALSE,因此如果存在任何冲突,它会在文件名末尾附加一个数字.有关参数的完整列表,请参阅本文顶部的链接.
| 归档时间: |
|
| 查看次数: |
35597 次 |
| 最近记录: |