如何在上传过程中重命名多个文件?代码点火器


How to rename more than 1 file during upload ? Codeigniter

我有一个表单要上传大约5个文件,比方说,这是一张正在注册的人的照片和其他关于他的文件。

现在我正在为每个注册用户创建一个带有他们名字的文件夹(顺便说一句,这样做好吗?),我想用正确的名字重命名每个文件,比如照片、文档a、文档B等等。

我当前的代码

        $upload_setting['upload_path'] = './uploads/'.$_POST['name'].'/';
        $upload_setting['allowed_types'] = 'jpg|pdf';
        $upload_setting['max_size'] = 5000; // 5 MB
        $upload_setting['file_name'] = $_POST['name']; // this define the uploaded file name

        $this->load->library('upload', $upload_setting);

        if ($this->form_validation->run() == FALSE){
            $this->load->view('form');
        }

        else{
            if (!is_dir('./uploads/'.$_POST['name'].'/')) {
                mkdir('./uploads/'.$_POST['name'].'/', 0777, TRUE);
            }

            $this->member->register($this->input->post(NULL, TRUE));
            $this->upload->do_upload('photo');
            $this->upload->do_upload('document-A');
            $this->upload->do_upload('document-B');

此外,我如何让他们免费上传JPG,PDF,DOC?我必须将这些文件链接到他们的个人资料页面。我怎么知道他们为每个文档上传的扩展名?

请让我知道你是否有任何建议或最佳实践来进行此上传,然后在他们的个人资料中显示每次上传。

感谢

要知道文件的扩展名,可以使用-

    $newname= "name";
    $path_parts = pathinfo($_FILES["picture"]["name"]);
    $file_newname = $newname.'.'.$path_parts['extension'];

上传-

if( $_FILES['picture']['name'] )
             {
                $config['upload_path'] = 'images/receipt/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['file_name'] = $file_newname; //Here your file is being renamed
                $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload('picture'))
                {
                    $error = array('error' => $this->upload->display_errors());
                    return false;
                }
             }