Search code examples
phpcodeignitercodeigniter-3

Save generated QR code image into path


I am generating QR code dynamically from a database, I need to store them in a specified path. when am generating the QR-code and saving individually, but now am generating them in bulk I need to store them in my preferred path. How can I achieve this?

This is my Function

function print_qr1(){
    $qr_code_config = array();
    $qr_code_config['cacheable']    = $this->config->item('cacheable');
    $qr_code_config['cachedir']     = $this->config->item('cachedir');
    $qr_code_config['imagedir']     = $this->config->item('imagedir');
    $qr_code_config['errorlog']     = $this->config->item('errorlog');
    $qr_code_config['ciqrcodelib']  = $this->config->item('ciqrcodelib');
    $qr_code_config['quality']      = $this->config->item('quality');
    $qr_code_config['size']         = $this->config->item('size');
    $qr_code_config['black']        = $this->config->item('black');
    $qr_code_config['white']        = $this->config->item('white');
    $this->ci_qr_code->initialize($qr_code_config);

    // get full name and user details
    $user_details['doctordata'] = $this->user->get_users();
    foreach ($user_details['doctordata'] as $value) {
        $image_name = $value->DOCTORCODE . ".png";

        $codeContents = "www.36d.co.in/qr_code=";
        $codeContents .= $value->DOCTORCODE;
        $codeContents .= "\n";

        $params['data'] = $codeContents;
        $params['level'] = 'H';
        $params['size'] = 10;

        $params['savename'] = FCPATH . $qr_code_config['imagedir'] . $image_name;
        $this->ci_qr_code->generate($params);

        $this->data['qr_code_image_url'] = base_url() . $qr_code_config['imagedir'] . $image_name;

        // save image path in tree table
        $this->user->change_userqr($value->DOCTORCODE);
        // then redirect to see image link
        $file = $params['savename'];
         function do_upload($file){
            $config['upload_path']          = './uploads/qrcode/';
            $config['allowed_types']        = 'png';
            $config['max_size']             = 0;
            $config['max_width']            = 0;
            $config['max_height']           = 0;

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

            if ( ! $this->upload->do_upload($file))
            {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $this->load->view('upload_success', $data);
            }
        }

Solution

  • Hope this wll help you :

    Your print_qr1 method should be like this :

    function print_qr1()
    {
        $qr_code_config = array();
        $qr_code_config['cacheable']    = $this->config->item('cacheable');
        $qr_code_config['cachedir']     = $this->config->item('cachedir');
        $qr_code_config['imagedir']     = $this->config->item('imagedir');
        $qr_code_config['errorlog']     = $this->config->item('errorlog');
        $qr_code_config['ciqrcodelib']  = $this->config->item('ciqrcodelib');
        $qr_code_config['quality']      = $this->config->item('quality');
        $qr_code_config['size']         = $this->config->item('size');
        $qr_code_config['black']        = $this->config->item('black');
        $qr_code_config['white']        = $this->config->item('white');
        $this->ci_qr_code->initialize($qr_code_config);
    
        // get full name and user details
        $user_details['doctordata'] = $this->user->get_users();
        foreach ($user_details['doctordata'] as $value) {
            $image_name = $value->DOCTORCODE . ".png";
    
            $codeContents = "www.36d.co.in/qr_code=";
            $codeContents .= $value->DOCTORCODE;
            $codeContents .= "\n";
    
            $params['data'] = $codeContents;
            $params['level'] = 'H';
            $params['size'] = 10;
    
            $params['savename'] = FCPATH . $qr_code_config['imagedir'] . $image_name;
            $this->ci_qr_code->generate($params);
    
            $this->data['qr_code_image_url'] = base_url().$qr_code_config['imagedir']. $image_name;
    
            // save image path in tree table
            $this->user->change_userqr($value->DOCTORCODE);
            // then redirect to see image link
            $file = $params['savename'];
            my_upload($file);
        }
    }
    

    This is your my_upload method

    function my_upload($file)
    {
        $config['upload_path']          = './uploads/qrcode/';
        $config['allowed_types']        = 'png';
    
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload($file))
        {
            $error = array('error' => $this->upload->display_errors());
    
            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
    
            $this->load->view('upload_success', $data);
        }
    }
    

    For more : https://www.codeigniter.com/user_guide/libraries/file_uploading.html