在CodeIgniter中重命名上传的文件 [英] Renaming an uploaded file in CodeIgniter

查看:143
本文介绍了在CodeIgniter中重命名上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using CodeIgniter, I am trying to modify the name of the uploaded file to camelCase by removing any spaces and capitalizing subsequent words.

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

I am pretty sure that I can rename the file using the second parameter of move_uploaded_file but I don't even know where to look to figure out how to modify the name to camelCase.

提前感谢!
Jon

Thanks in advance! Jon

推荐答案

查看CI的上传库:

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

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

Let's first take a look at how to do a simple file upload without changing the filename:

$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();
}

这很简单,效果非常好。

It's that simple and it works quite well.

现在,让我们来看看你的问题的肉。首先我们需要从$ _FILES数组中获取文件名:

Now, let's take a look at the meat of your problem. First we need to get the file name from the $_FILES array:

$file_name = $_FILES['file_var_name']['name'];

然后我们可以用 _ delimiter like this:

Then we can split the string with a _ delimiter like this:

$file_name_pieces = split('_', $file_name);

然后我们必须遍历列表并创建一个新的字符串,有大写字母:

Then we'll have to iterate over the list and make a new string where all except the first spot have uppercase letters:

$new_file_name = '';
$count = 1;

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

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



现在我们有了新的文件名,我们可以重温我们上面做的。基本上,除了你添加这个$ config param,你做的一切都是一样的:

Now that we have the new filename, we can revisit what we did above. Basically, you do everything the same except you add this $config param:

$config['file_name'] = $new_file_name;

这应该可以!默认情况下,CI将覆盖 $ config param设置为 FALSE ,因此如果有任何冲突,一个数字到您的文件名的结尾。有关参数的完整列表,请参阅本文顶部的链接。

And that should do it! By default, CI has the overwrite $config param set to FALSE, so if there are any conflicts, it will append a number to the end of your filename. For the full list of parameters, see the link at the top of this post.

这篇关于在CodeIgniter中重命名上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆