prakashchhetri
prakashchhetri

Reputation: 1816

codeigniter uploads directory, unlink photo

I am using codeigniter to create the admin panel of my website. I am not using it for the front end because I have lots of static pages in the front end and only a few things need to be dynamic so I will be doing it with core PHP queries.

My link for unlinking photo is , images is the controller and unlinkPhoto is the function and 32 is the image ID.

localhost/admin/index.php/images/unlinkPhoto/32

EDIT

however my image is located at localhost/uploads/testimage.jpg. How can I point to that folder to unlink the image in codeigniter.

Upvotes: 1

Views: 1626

Answers (1)

hohner
hohner

Reputation: 11588

You really need to make sure you're enforcing decent security protocols, otherwise anyone can fake the GET request and delete the entirety of your uploaded files. Here is a basic solution:

public function unlinkPhoto($photoId)
{
   // Have they specified a valid integer?
   if ((int) $photoId > 0) {
      foreach (glob("uploads/*") as $file) {
         // Make sure the filename corresponds to the ID
         // Caters for all file types (not just JPGs)
         $info = pathinfo($file);
         if ($info['filename'] == $photoId) {
            unlink($file);
         }
      }
   }
}

If you're using PHP 5.4, you might be able to reduce this code down even more:

if (pathinfo($file)['filename'] == $photoId) {
   unlink($file);
}

Because they've implemented array dereferencing (finally). Although I haven't tested this particular piece of code. This is just a geeky addendum.

Upvotes: 3

Related Questions