• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting encrypted name problem.

#1
Hey guys,
I've created the following functions which do the following:

1) Creating a dir in server with a unique id,
2) encrypting file name
3) resizing the photo
4) uploading the data in db.

My problem is that i can't get the file name in the db, even though the dir folder is created and the other data are passed. My guess is that i messed up the upload image variables a bit.... 

Any ideas or suggestions on that (how to solve it or making the code more clean) ?!

Thank you!!

Controller
Code:
public function found(){

   if ($this->session->userdata('logged_in') == true) {

     $user_id = $this->session->userdata('user_id');

     $data['user'] = $this->user_model->get_user_data($user_id);
   }

   $this->form_validation->set_rules('petTitle_found', 'Entry title', 'required');

   if($this->form_validation->run() === FALSE){
     $this->load->view('pets/found', $data);
   } else {
     $pet_entry_id = random_string('numeric', 6);

     //Image uploading
     $config['encrypt_name'] = TRUE;
     $config['upload_path'] = './assets/img/pets' . $pet_entry_id;
     $config['allowed_types'] = 'gif|jpg|jpeg|png';
     $config['max_size'] = '204800';
     $config['max_width'] = '2048 ';
     $config['max_height'] = '2048';

     $image_data = array();

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

     if ( !is_dir('./assets/img/pets/' . $pet_entry_id) ) {
       mkdir('./assets/img/pets/' . $pet_entry_id, 0777, true);
     } else {

     }

     if (!$this->upload->do_upload('userfile')) {
       $errors = array('error' => $this->upload->display_errors());
       $config['file_name'] = 'default_petavatar.png';
     } else {
       //store the file info
       $image_data = $this->upload->data();
       $config['image_library'] = 'gd2';
       $config['source_image'] = $image_data['full_path'];
       $config['maintain_ratio'] = TRUE;
       $config['width'] = 550;
       $config['height'] = 450;
       $this->load->library('image_lib', $config);
       if (!$this->image_lib->resize()) {
         $this->handle_error($this->image_lib->display_errors());
       }
     }

     $this->pet_model->create_found_pet($pet_entry_id);
     redirect('pets');
   }
 }

Model
Code:
public function create_found_pet($pet_entry_id){

     if ($this->session->userdata('logged_in') == true) {

       $user_id = $this->session->userdata('user_id');

     } else {
       $data_user = array(
         'screen_name' => $this->input->post('person_name_found'),
         'email' => $this->input->post('person_email_found')
       );

     $this->db->insert('users', $data_user);
     $user_id = $this->db->insert_id();
     }

     $pet_entry = convert_accented_characters($this->input->post('petTitle_found'));
     $pet_entry_slug = url_title($pet_entry);

     $data_pet = array(
         'pet_entry_title' => $this->input->post('petTitle_found'),
         'pet_entry_id' => $pet_entry_id,
         'category_id' => $this->input->post('petCategory_found'),
         'pet_color' => $this->input->post('petColor_found'),
         'pet_descr' => $this->input->post('petMoreInfo_found'),
         'pet_photo' => $this->upload->data('file_name'),
         'pet_entry_slug' => $pet_entry_slug,
         'pet_status' => 2,
         'user_id' => $user_id
     );

     return $this->db->insert('pets', $data_pet);
   }

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#2
You need to start putting echo/var_dump/print_r and combined it with exit; from start to bottom. To see where you messed up.
We can technically find it for you, but it will be much more useful for future developing of your skills to be able to debug your own code.
Reply

#3
Here is the code to a better var_debug.

Just place it in a helper and run  it.

PHP Code:
if ( ! function_exists('varDebug'))
{
    
/**
     * Debug Helper
     * ----------------------------------------------------------------------------
     * Outputs the given variable(s) with color formatting and location
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.$callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument)
        {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";
        exit;
    }


Hope that helps
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
(08-17-2018, 02:10 AM)jreklund Wrote: You need to start putting echo/var_dump/print_r and combined it with exit; from start to bottom. To see where you messed up.
We can technically find it for you, but it will be much more useful for future developing of your skills to be able to debug your own code.

Totally agree with that, but since i've never used it (and that was my bad) could give an example of how to use it, since i couldn't find any helpful tuts online?!

P.S. self-taught here, so if there are any silly questions....!  Big Grin

Thank you!!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#5
PHP Code:
$pet_entry_id random_string('numeric'6);
var_dump($pet_entry_id);exit; 
If that looks you good you move on to the next one.
PHP Code:
$config['upload_path'] = './assets/img/pets' $pet_entry_id;
var_dump($config['upload_path']);exit; 

In the end you have placed a debug and exit (it will stop rendering) on every row. So you know where it dosen't have it's information anymore.

There are a logging feature in Codeigniter too, in case you want it logged to a file instead.
https://www.codeigniter.com/user_guide/g...og_message

Or did you mean this value?
PHP Code:
'pet_photo' => $this->upload->data('file_name'), 
Reply

#6
(08-17-2018, 07:11 AM)jreklund Wrote:
PHP Code:
$pet_entry_id random_string('numeric'6);
var_dump($pet_entry_id);exit; 
If that looks you good you move on to the next one.
PHP Code:
$config['upload_path'] = './assets/img/pets' $pet_entry_id;
var_dump($config['upload_path']);exit; 

In the end you have placed a debug and exit (it will stop rendering) on every row. So you know where it dosen't have it's information anymore.

There are a logging feature in Codeigniter too, in case you want it logged to a file instead.
https://www.codeigniter.com/user_guide/g...og_message

Or did you mean this value?
PHP Code:
'pet_photo' => $this->upload->data('file_name'), 
I see! I will test it in "battle conditions" and i will inform you! 
The answer though was much simpler than it looked like. I only had to think more clear. 

At first I declared a wrong variable for the default image upload in case no files were uploaded. 
Moreover I was not passing the right attributes to the upload data model. Tbh i wasn't passing the image attribute to the model, at all!!

I added 3 lines and everything solved! Check it out and tell me your opinion!
Code:
public function found(){

   $this->form_validation->set_rules('petTitle_found', 'Title name', 'required');

   if($this->form_validation->run() === FALSE){

   } else {

     $pet_entry_id = random_string('numeric', 6);

     //Image uploading
     $config = array(
       'upload_path' => './assets/img/pets/' . $pet_entry_id,
       'encrypt_name' => TRUE,
       'allowed_types' => 'gif|jpg|jpeg|png',
       'max_size' => '204800',
       'max_width' => '2048',
       'max_height' => '2048'
     );

     $image_data = array();

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

     $dir_exist = true; //Default flag for dir existence

     if ( !is_dir('assets/img/pets/' . $pet_entry_id) ) {
       mkdir('./assets/img/pets/' . $pet_entry_id, 0777, true);
       $dir_exist = false; // dir not exist
     }

     if (!$this->upload->do_upload()) {
       if(!$dir_exist)
         rmdir('./assets/img/pets/' . $pet_entry_id);

       $errors = array('error' => $this->upload->display_errors());
       $image = 'default_petavatar.png';
     } else {
       $image_data = $this->upload->data();

       $config['image_library'] = 'gd2';
       $config['source_image'] = $image_data['full_path'];
       $config['maintain_ratio'] = TRUE;
       $config['width'] = 250;
       $config['height'] = 250;

       $this->load->library('image_lib', $config);
       if ( ! $this->image_lib->resize()){
         echo $this->image_lib->display_errors();
       }

       $data = array('upload_data' => $this->upload->data());
       $image = $this->upload->data('file_name');
     }


     $this->pet_model->create_found_pet($image, $pet_entry_id);
     redirect('pets');
   }

   $this->load->view('templates/header', $data);
   $this->load->view('pets/found', $data);
   $this->load->view('templates/footer', $data);
 }


Thank you very much!
I appreciate your overall help!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#7
For  echo it would be:

PHP Code:
echo $data;
echo 
$month

For var_dump it would be like:

PHP Code:
var_dump($data);
var_dump($month); 

You can use the helper method I posted above to get a nice output of the dump that you can read.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
Great that you got it all sorted out. :-) Here are some intakes on your code.
I would make sure that you don't receive an error on mkdir. Exit and give the user an error. Or use a fallback folder and log it for an admin that you got an problem.
PHP Code:
mkdir('./assets/img/pets/' $pet_entry_id0777true); 

Same as the above, but only log it for the admin.
PHP Code:
rmdir('./assets/img/pets/' $pet_entry_id); 

Where are you using this one? You are still saving "default_petavatar" correct?
PHP Code:
$errors = array('error' => $this->upload->display_errors()); 

Are this something critical for the user to see? Also that should be passed in flashdata() as you are doing a redirect later. If not log it for an admin.
PHP Code:
echo $this->image_lib->display_errors(); 

As far as I can tell this will never be seen. You are doing a redirect later on.
PHP Code:
$data = array('upload_data' => $this->upload->data()); 

Make sure this are returning true for success or false otherwise. If true = redirect. False = give user all those error messages you want.
PHP Code:
$this->pet_model->create_found_pet($image$pet_entry_id); 
Reply

#9
(08-17-2018, 11:02 AM)jreklund Wrote: Great that you got it all sorted out. :-) Here are some intakes on your code.
I would make sure that you don't receive an error on mkdir. Exit and give the user an error. Or use a fallback folder and log it for an admin that you got an problem.
PHP Code:
mkdir('./assets/img/pets/' $pet_entry_id0777true); 

Same as the above, but only log it for the admin.
PHP Code:
rmdir('./assets/img/pets/' $pet_entry_id); 

Where are you using this one? You are still saving "default_petavatar" correct?
PHP Code:
$errors = array('error' => $this->upload->display_errors()); 

Are this something critical for the user to see? Also that should be passed in flashdata() as you are doing a redirect later. If not log it for an admin.
PHP Code:
echo $this->image_lib->display_errors(); 

As far as I can tell this will never be seen. You are doing a redirect later on.
PHP Code:
$data = array('upload_data' => $this->upload->data()); 

Make sure this are returning true for success or false otherwise. If true = redirect. False = give user all those error messages you want.
PHP Code:
$this->pet_model->create_found_pet($image$pet_entry_id); 

Thank you for pointing out the mistakes here! I'll recheck it and i will inform you as soon as i have any further issues!  Smile

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#10
(08-17-2018, 10:21 AM)InsiteFX Wrote: For  echo it would be:

PHP Code:
echo $data;
echo 
$month

For var_dump it would be like:

PHP Code:
var_dump($data);
var_dump($month); 

You can use the helper method I posted above to get a nice output of the dump that you can read.

Oh i see! So if anything comes up with my code, i will use this and i will inform you!
Thank you for your help! Smile

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.