PHP笨上传类,重命名特定模式文件(PHP Codeigniter Uploading Class,

2019-09-23 05:14发布

我有麻烦与笨一个点,获取文件从上传库上载过程中它提供中重命名。 现在,任何人说,这之前,我不找“加密”文件名。

我的问题是上传图片,你有你可能要处理类型的好屈指可数。 那么,如何使用一个更改文件名file_name配置选项以一个特定的模式(我已经有模式部件并工作)。 但保持相同的文件类型?

现在,我尝试

$upload_config['file_name'] = $generated_filename_from_schema

唯一的问题是$generated_filename_from_schema可是没有一个文件扩展名,并留下文件扩展名的方程CI似乎完全忽略了它,并只需要文件和append_1,_2,_3,因为它去了,如果文件具有相同的名称,否则它只是离开名称不变。

现在我已经传了$config到CI因此将上传的文件,但我怎么能determin什么样的文件,我用它改掉上传,所以我可以用我的名字代架构之前的工作。

* 编辑 *

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

Answer 1:

你可以用$ _FILES数组获取文件的原始名称

提取原始file.Then的扩展,追加到新的文件名。

尝试如下

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


Answer 2:

就个人而言,我觉得CodeIgniter的文件上传类是有点麻烦。 如果你想有一个香草PHP的解决方案:

function submit_image(){
    $f = $_FILES['image'];
    $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
    $detectedType = exif_imagetype($f['tmp_name']);
    if(in_array($detectedType, $allowedTypes)){
        $pi = pathinfo($f['name']);
        $ext = $pi['extension'];
        $target = $this->genfunc->genFileName($uid) "." . $ext;
        if(move_uploaded_file($f['tmp_name'], $target)){
            /*success*/
        }
        else {/*couldn't save the file (perhaps permission error?*/}
    }
    else {/*invalid file type*/}
}


文章来源: PHP Codeigniter Uploading Class, rename files with specific schema