• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to show an error if there is no folder.

#1
[eluser]shinokada[/eluser]
I have the following function in a model.

The function works ok. But if there is no folder it does not show any error.

Could anyone suggest me how to show the error if there is no folder?


Code:
class MProducts extends Model{
function MProducts(){
    parent::Model();
}
...
...
/**
* This will add product
*/

function addProduct(){
    $data = $this->_uploadFile();
    $this->db->insert('omc_products', $data);

    ...
    ...    
}
/**
* This will edit the product.
*/
function updateProduct(){
    $data = $this->_uploadFile();
     $this->db->where('id', $_POST['id']);
    $this->db->update('omc_products', $data);    
    $this->db->where('product_id', $_POST['id']);
    $this->db->delete('omc_products_colors');
    $this->db->where('product_id', $_POST['id']);
    $this->db->delete('omc_products_sizes');
     ...
     ...
}

/**
  * This function will upload image and thumbnail and return $data['image']
  * and $data['thumbnail'] and other $_POST details data
  *
  */
function _uploadFile(){
  $data = array(
        'name' => db_clean($_POST['name']),
        'shortdesc' => db_clean($_POST['shortdesc']),
        'longdesc' => db_clean($_POST['longdesc'],5000),
        'status' => db_clean($_POST['status'],8),
        'class' => db_clean($_POST['class'],30),
        'grouping' => db_clean($_POST['grouping'],16),
        'category_id' => id_clean($_POST['category_id']),
        'featured' => db_clean($_POST['featured'],20),
        'price' => db_clean($_POST['price'],16),
        'other_feature' => db_clean($_POST['other_feature'],20)
    
    );
  $catname = array();
        $category_id = $data['category_id'];
        $catname = $this->MCats->getCategoryNamebyProduct($category_id);
        foreach ($catname as $key => $name){
        $foldername = strtolower($name);
        $foldername = str_replace(" ", "_", $foldername);
        }
      
    if ($_FILES){
        $config['upload_path'] = './images/'.$foldername.'/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200';
        $config['remove_spaces'] = true;
        $config['overwrite'] = true;
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        
        $this->load->library('upload', $config);    
        if (strlen($_FILES['image']['name'])){
            if(!$this->upload->do_upload('image')){
                $this->upload->display_errors();
                exit();
            }
            $image = $this->upload->data();
            if ($image['file_name']){
                $data['image'] = "images/".$foldername."/".$image['file_name'];
            }
        }
        
        $config['upload_path'] = './images/'.$foldername.'/thumbnails/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200';
        $config['remove_spaces'] = true;
        $config['overwrite'] = true;
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        //initialize otherwise thumb will take the first one
        $this->upload->initialize($config);
        
        if (strlen($_FILES['thumbnail']['name'])){
            if(!$this->upload->do_upload('thumbnail')){
                $this->upload->display_errors();
                exit();
            }
            $thumb = $this->upload->data();
            if ($thumb['file_name']){
                $data['thumbnail'] = "images/".$foldername."/thumbnails/".$thumb['file_name'];
            }
        }
    }
    return $data;
}

#2
[eluser]pistolPete[/eluser]
You can use the PHP function is_dir(): http://php.net/manual/en/function.is-dir.php

Code:
if( !is_dir('./images/'.$foldername.'/') )
{
   // check the return value in your controller; if model returns FALSE, show an error
   return FALSE;
}
else
{
   // upload
}

#3
[eluser]shinokada[/eluser]
Thanks. I will try it now.

Do you think I need to move some code to controller?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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