PHP Codeigniter Uploading Class, rename files with specific schema

梦想的初衷 提交于 2019-12-05 22:50:29

You can use $_FILES array to get original name of file.

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

Try as below

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

Personally, I find CodeIgniter's file uploading class to be somewhat cumbersome. If you want a vanilla PHP solution:

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*/}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!