首页 > 解决方案 > i am new in php i need to upload file with random name

问题描述

I am new in php i need to upload file with random name assigning to the file and store that file with random name to upload folder and store that random name into mysql database.

  $pic_file1 = $this->input->post('pic_file');

    $pic_file1 = str_replace( "\\", '/', $pic_file1);
    $filename = time().basename($pic_file1);


            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 1000;
            //$config['encrypt_name'] = TRUE;
            // $config['overwrite'] = FALSE; 
            $config['file_name'] =  $filename;          

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

            if ( ! $this->upload->do_upload('pic_file'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                   // print_r($data);                       

            }

标签: phpmysqlcodeigniter

解决方案


In your code just uncomment $config['encrypt_name'] = TRUE; 
then automatically your file name store in random name formate or jsut copy below code

 $pic_file1 = $this->input->post('pic_file');

    $pic_file1 = str_replace( "\\", '/', $pic_file1);
    $filename = time().basename($pic_file1);


            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 1000;
            $config['encrypt_name'] = TRUE;
            // $config['overwrite'] = FALSE; 
            $config['file_name'] =  $filename;          

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

            if ( ! $this->upload->do_upload('pic_file'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                   // print_r($data);                       

            }




推荐阅读