⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Callback before delete file, delete thumbnail



Alejandra Restrepo

Alejandra Restrepo
  • profile picture
  • Member

Posted 08 October 2012 - 04:35 AM

Hello from Colombia,

How I can to delete a thumbnail file (Example: "picture_thumb.jpg") in another folder when I delete a file (Example: "picture.jpg") in the edit form? Tanks a lot! :)

victor

victor
  • profile picture
  • Member

Posted 08 October 2012 - 06:24 AM

Hi!


$crud->callback_before_delete(array($this,'delete_thumb'));
.....

function delete_thumb($primary_key)
{
// get your the images name
$image = $this->db->get_where('images', array('id'=>$primary_key), 1)->row_array();
if
(
unlink($your_path.'/thumb_'.$image['name'])
)
return true
else
return false
}


zgames

zgames
  • profile picture
  • Member

Posted 19 February 2013 - 23:46 PM

Hello Victor, I'm interested to know how to correctly unlink, if my thumbnails are at the htdocs/webroot/assets/uploads/files/thumbnail/image_file 

 

I'm trying to give it a correct base path

 

something like this:

 $my_path = '/assets/uploads/files/thumbnail/'.$image['file_url'];

or as

 $my_path = '/webroot/assets/uploads/files/thumbnail/'.$image['file_url'];

or as 

 $my_path = '/htdocs/webroot/assets/uploads/files/thumbnail/'.$image['file_url'];

 

and than calling it like this

if(unlink($my_path))
        {
            return true;
        }
          else 
          {
            return false;
          }

 

 

and my image still wasn't deleted, is it possible that my .htaccess is somehow involved in all of this? maybe I need to give 777 permission to my images?  which i don't know how...

 

 

my full function:

function delete_thumbnail($primary_key)
    {
        // get images name
        $image = $this->db->get_where('cars', array('item_id'=>$primary_key), 1)->row_array();
        $filestring = '/test/assets/uploads/files/thumbnail/'.$image['file_url'];

        if(unlink($filestring))
        {
            return true;
        }
          else 
          {
            return false;
          }
    }

 

my callback

$crud->callback_before_delete(array($this,'delete_thumbnail'));

 

 

.htaccess

<IfModule mod_rewrite.c>
 
    RewriteEngine On
    RewriteBase /test/
 
    ### Canonicalize codeigniter URLs
 
    # If your default controller is something other than
    # "standard_library" you should probably change this
    RewriteRule ^(standard_library(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]
 
    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]
 
    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    #RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
    #RewriteRule ^(.*)$ http://localhost/test/$1 [L,R=301]
 
    # Enforce NO www
    #RewriteCond %{HTTP_HOST} ^www [NC]
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
 
    ###
 
    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
 
    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
 
</IfModule>
 
<IfModule !mod_rewrite.c>
 
    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php
 
</IfModule>

pbcomput

pbcomput
  • profile picture
  • Member

Posted 24 December 2013 - 15:42 PM

Hi,

 

I am trying to remove thumbnail which I store in same folder. I code like below, but its not removing thumbnail when I delete the image. 

 

my code in function

$crud->callback_before_delete(array($this,'delete_thumb'));

callback function code

public function delete_thumb($primary_key)
	{
	    $row = $this->db->where('member_id',$primary_key)->get('member')->row();

	    unlink('/assets/uploads/files/thumb1-'.$row->profile_photo);
	   
	    return true;
	}

If anyone have solution for this, please help me to solve the issue.

 

Thanks. :)


pbcomput

pbcomput
  • profile picture
  • Member

Posted 31 December 2013 - 17:17 PM

Finally, I got the solution. which is as below. 

function delete_thumb($primary_key)
	{
		$user= $this->db->where('member_id',$primary_key)->get('member')->row();
  
		$url= 'assets/uploads/files/'.$user->profile_photo;
		$thumb_url= 'assets/uploads/files/thumb1-'.$user->profile_photo;
		unlink($url);
		unlink($thumb_url);

	   
	    return true;
	}

I hope it may help someone who is facing issue for deleting image.

 

Thanks