Welcome Guest, Not a member yet? Register   Sign In
Upload multiple file code optimization
#1

[eluser]Shinamee[/eluser]
Hello everyone,

I know this code is not properly optimized and that is why I am here for advise on how to make it better, I have limited knowledge in server-side programming and this is my first e-commerce application.

Please kindly advise me on how to improve this code, uploading the form and files takes about 60seconds but I think it can be faster. What am I doing wrong?

Code:
function upload_theme()
{
  // get the current logged user by array and id
  $user = $this->flexi_auth->get_user_by_id_row_array();
  $data['user_id'] = (! empty($user)) ? $user['uacc_id'] : null;
  $data['user_username'] = (! empty($user)) ? $user['uacc_username'] : null;
  $data['user_email'] = (! empty($user)) ? $user['uacc_email'] : null;

  // If the add post has been submitted
  if ($this->input->post('upload_theme'))
  {
                        // function for generating random key
   $unique = $this->generate_random_letters(10);

   $this->db->select("theme_reference");
   $this->db->from('themes');
   $this->db->where('theme_reference', $unique);
         $query = $this->db->get();

         if ($query->num_rows() > 0)
         {
             $unique = $this->generate_random_letters(10);
      }
      else
      {
    $themeTitle = $this->input->post('create_title');
    $userEmail = $data['user_email'];
    $userName = $data['user_username'];
    $user_id = $data['user_id'];

    // image
    if (!empty($_FILES['userfile1']['name']))
    {
     // for image preview
     // check if the folder already exist
     $folderName = "./preview/".$unique;

     if (!is_dir($folderName))
     {
      // create a new image folder is the user currently dont have one
      $newFolderName = mkdir("./preview/".$unique, 0777);
      $config['upload_path'] =  $folderName;
     }
     else
     {
      // if the user already has a folder then upload new image to it
      $config['upload_path'] =  $folderName;
     }

     $config['allowed_types'] = 'jpg|png';
     $config['max_size'] = '0';
     $config['max_width']  = '730';
     $config['max_height']  = '500';
     $config['overwrite'] = FALSE; //If the file exists it will be saved with a progressive number appended

     // Initialize the new config
              $this->upload->initialize($config);

              // Upload the second file
              if ($this->upload->do_upload('userfile1'))
              {
                  $dataImage = $this->upload->data();
                  $image_data = $dataImage;

       $image_array = array(
        'preview_uacc_fk' => $user_id,
        'preview_theme_reference' => $unique,
                      'preview_theme_name' => $image_data['file_name'],
                      'preview_theme_path' => $image_data['full_path']
                  );

       // first insert the image data into the previews table to save the record
           $this->db->insert('previews', $image_array);

           // zip
      if (!empty($_FILES['userfile2']['name']))
      {
       // for zip files
       // check if the folder already exist
       $folderName = "./preview/".$unique;

       if (!is_dir($folderName))
       {
        // create a new image folder is the user currently dont have one
        $newFolderName = mkdir("./preview/".$unique, 0777);
        $config['upload_path'] =  $folderName;
       }
       else
       {
        // if the user already has a folder then upload new image to it
        $config['upload_path'] =  $folderName;
       }

       $config['allowed_types'] = 'zip';
       $config['max_size'] = '0';
       $config['overwrite'] = FALSE; //If the file exists it will be saved with a progressive number appended

       // Initialize the new config
                $this->upload->initialize($config);

                // Upload the second file
                if ($this->upload->do_upload('userfile2'))
                {
                    $dataZip = $this->upload->data();
                    $zip_data = $dataZip;

                 // Give it one parameter and it will extract to the same folder
        $this->unzip->extract('preview/' . $unique . '/' . $zip_data['file_name']);

         $zip_array = array(
          'zip_uacc_fk' => $user_id,
                        'zip_theme_name' => $zip_data['file_name'],
                        'zip_theme_path' => $zip_data['full_path'],
                        'zip_theme_reference' => $unique
                    );

         // insert the zip file data into the zips table to save the record
             $this->db->insert('zips', $zip_array);

           // then insert the newly created theme into the themes table.
        $this->auth_model->upload_theme($image_data['file_name'], $unique);
                }
                else
                {
                    $data['uploadmessage'] = "Error with your zip file, please try uploading the correct format.";
                }
      }
              }
              else
              {
                  $data['uploadmessage'] = "Error with your image, please try uploading the correct format.";
              }
    }
   }
  }
  else
  {
   redirect('dashboard/upload', $data);
  }
}
#2

[eluser]Shone[/eluser]
Sorry for abusing this thread, but for some reason I can not make my own thread. Some hilarious spam protection do not allow me to.

I have a problem with file upload class.

Can anyone tell me when I will be able to post new thread, or I can use this one to ask question ?




Theme © iAndrew 2016 - Forum software by © MyBB