CodeIgniter Forums
Stop user from deleting a set file name not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Stop user from deleting a set file name not working (/showthread.php?tid=63237)



Stop user from deleting a set file name not working - wolfgang1983 - 10-10-2015

I have a file in two locations called no_image.jpg When user try's to delete the image I would like to be able to stop the user from deleting just that image.

But for some reason the image still gets delete How can I make it stop deleting image if file is no_image.png


PHP Code:
public function delete() {
 
       $json = array();

 
       if (!$json) {

 
           foreach ($this->input->post('path') as $path) {
 
               $no_image_check $this->config->item('image_path') . 'catalog/no_image.jpg';
 
               $no_image_cache_check $this->config->item('image_cache_path') . 'catalog/no_image.jpg';

 
               if (isset($no_image_check)) {
 
                   $json['error'] = 'You cannot remove this image';
 
               }

 
               if (isset($no_image_cache_check)) {
 
                   $json['error'] = 'You cannot remove this image';
 
               }

 
               if (is_dir($this->config->item('image_path') . $path)) {
 
                   rmdir($this->config->item('image_path') . $path);
 
                   rmdir($this->config->item('image_cache_path') . $path);
 
               } elseif (is_file($this->config->item('image_path') . $path)) {
 
                   unlink($this->config->item('image_path') . $path);
 
                   unlink($this->config->item('image_cache_path') . $path);
 
               }
 
           }

 
           $json['success'] = 'Your Selected File Has Been Removed!';
 
       }

$this->output->set_content_type('Content-Type: application/json');
$this->output->set_output(json_encode($json));



Any examples on how to solve it would be great.


RE: Stop user from deleting a set file name not working - JayAdra - 10-10-2015

It looks like your "validation" is just checking if the variable $no_image_check is set, which it is on the previous line.

What you want to do is check if the actual file exists:
http://php.net/manual/en/function.file-exists.php


RE: Stop user from deleting a set file name not working - wolfgang1983 - 10-10-2015

(10-10-2015, 12:29 AM)JayAdra Wrote: It looks like your "validation" is just checking if the variable $no_image_check is set, which it is on the previous line.

What you want to do is check if the actual file exists:
http://php.net/manual/en/function.file-exists.php

I have check boxes below each image though and user needs to have that check box checked before delete but need to make sure does not delete the no_image.jpg.

I am also not sure if I have my json message in correct places.


PHP Code:
    public function delete() {

        $json = array();

        if (!$json) {

            foreach ($this->input->post('path') as $path) {
                $no_image_check $this->config->item('image_path') . 'catalog/no_image.jpg';

                if (isset($no_image_check)) {
                    
                    $json
['error'] = 'You canot remove this image';

                } else {
                    
                    
if (is_dir($this->config->item('image_path') . $path)) {

                        if (file_exists($this->config->item('image_path') . $path)) {
                            delete_files($this->config->item('image_path') . $path);
                            delete_files($this->config->item('image_cache_path') . $path);
                        

                        continue;

                        rmdir($this->config->item('image_path') . $path);
                        rmdir($this->config->item('image_cache_path') . $path);

                        $json['success'] = 'Your Selected Folder Has Been Removed!';

                    } elseif (is_file($this->config->item('image_path') . $path)) {

                        delete_files($this->config->item('image_path') . $path);
                        delete_files($this->config->item('image_cache_path') . $path);

                        $json['success'] = 'Your Selected File Has Been Removed!';
                
                    
}
                }
            }

        }

        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));

    



RE: Stop user from deleting a set file name not working - wolfgang1983 - 10-10-2015

After some more thinking about it I have got it all most working now.

In my foreach loop I have done a check like below.

if ($this->config->item('image_path') . $path == $no_image_check) {



PHP Code:
   public function delete() {
 
       $json = array();

 
       if (!$json) {

 
           $no_image_check $this->config->item('image_path') . 'catalog/no_image.jpg';

 
           foreach ($this->input->post('path') as $path) {

 
               if ($this->config->item('image_path') . $path == $no_image_check) {

 
                   $json['error'] = 'Your Selected File Can\'t be removed!';

 
               } else {

 
                   if (is_dir($this->config->item('image_path') . $path)) {

 
                       rmdir($this->config->item('image_path') . $path);
 
                       rmdir($this->config->item('image_cache_path') . $path);

 
                       $json['success'] = 'Your Selected Folder Has Been Removed!';

 
                   } elseif (is_file($this->config->item('image_path') . $path)) {

 
                       unlink($this->config->item('image_path') . $path);
 
                       unlink($this->config->item('image_cache_path') . $path);

 
                       $json['success'] = 'Your Selected File Has Been Removed!';

 
                   }
 
               }
 
               
            
}
 
           
        
}

 
   $this->output->set_content_type('Content-Type: application/json');
 
  $this->output->set_output(json_encode($json));