Welcome Guest, Not a member yet? Register   Sign In
Stop user from deleting a set file name not working
#1

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.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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
Reply
#3

(This post was last modified: 10-10-2015, 12:59 AM by wolfgang1983.)

(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));

    


Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

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));


There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB