PHP Codeigniter上载类,使用特定架构重命名文件 [英] PHP Codeigniter Uploading Class, rename files with specific schema

查看:59
本文介绍了PHP Codeigniter上载类,使用特定架构重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Codeigniter时遇到了麻烦,并需要从其提供的上载库中在上载过程中重命名文件。现在,在任何人说出来之前,我都没有在寻找加密的文件名。

I am having a spot of trouble with Codeigniter and getting files to rename within the upload process from the Upload Library it has to offer. Now before anyone says it, I am not looking for "encrypted" file names.

我的问题是在上传图像时,您会处理很多类型的图像。因此,如何使用 file_name config选项将文件名更改为特定的架构(我已经完成了架构部分的工作)。但是保持相同的文件类型吗?

My Problem is in uploading images you have a good handful of types you could be dealing with. So how does one change the file name using the file_name config option to a specific schema (which I already have the schema part up and working). But maintain the same file type?

现在我正在尝试

$ upload_config ['file_name'] = $ generation_filename_from_schema

唯一的问题是 $ generation_filename_from_schema 没有一个文件扩展名,而将文件扩展名放在等式CI之外似乎完全忽略了它,并且如果文件具有相同的名称,则会直接使用该文件和append_1,_2,_3,否则它将保持名称不变。

Only problem is $generated_filename_from_schema doesnt have a file extension, and leaving the file extension out of the equation CI seems to ignore it altogether and just takes the file and append_1, _2, _3 as it goes up if the files have the same name, otherwise it just leaves the name intact.

现在,我必须将 $ config 传递给CI,以便它将文件上传,但是如何确定在尝试上传哪种类型的文件之前,我可以使用我的名称生成架构。

Now I have to pass the $config to CI so it will upload the file, but how can I determin what kind of file I am working with before it trys to upload so I can use my name generation schema.

* edit *

*edit*

    $upload_config['upload_path'] = realpath(APPPATH.'../images/');
    $upload_config['allowed_types'] = 'gif|jpg|png';
    $upload_config['max_size']  = 0;
    $upload_config['max_width'] = 0;
    $upload_config['max_height'] = 0;
    $upload_config['remove_spaces'] = true;

    $upload_config['file_name'] = $this->genfunc->genFileName($uid);

    if($this->input->post('uploads'))
    {

        $this->load->library('upload');
        $this->upload->initialize($upload_config);

        if (!$this->upload->do_upload())
        {
            //echo 'error';
            echo $config['upload_path'];
            $this->data['errors'] = $this->upload->display_errors();
        }
        else
        {
            //echo 'uploaded';
            $this->data['upload_data'] = $this->upload->data();
        }
    }


推荐答案

您可以使用 $ _ FILES数组获取文件的原始名称

提取原始文件的扩展名,然后附加到新文件名。

Extract extension of original file.Then, append to your new file name.

尝试如下

$ext = end(explode(".", $_FILES[$input_file_field_name]['name']));
$upload_config['file_name'] = $this->genfunc->genFileName($uid).'.'.$ext;

这篇关于PHP Codeigniter上载类,使用特定架构重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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