1

I am trying to delete all user data including all images from 'uploads' folder when deleted a record in XAMPP. So far it is deleting rows from table but for the images from upload folder I get this error,

Message: unlink(uploads/property-images/): Permission denied

When I print_r($rowId); from model it echo the images array fine. I am sure that I may be doing something wrong in model as delete function has to be an array on it but I am not sure how to do that.

But at the same time in my other scripts I have successfully deleted and replace user's profile picture using unlink.. I guess if that worked than 'Permission' shouldn't be the problem this time.. The only difference is that this time I have an array of images exploded in script.

In my controller:

function delete_listing_id() {
    if($this->session->userdata('is_logged_in')){
    $id = $this->uri->segment(3);
    $images_urls = $this->my_listings_model->get_property_all_images_url($id);
    $this->my_listings_model->delete_listing_images($images_urls);

    $this->my_listings_model->delete_listing_id($id);

    $this->my_listings();
}

Model:

function get_property_all_images_url($id) {
    $this->db->select('property_images');
    $this->db->from('vbc_property_images');
    $this->db->where('property_ref_id', $id);
    $query = $this->db->get();

$query_result = $query->result_array();
if (empty($query_result)) {
    return FALSE;
}
elseif (count($query_result) > 1) {
    return 0;
}
else{
    $rowId = explode(',',$query_result[0]['property_images']);
    return $rowId;    
    }
}

function delete_listing_id($id){
    $this->db->delete('vbc_vacation_item_attri', array('vbc_item_id' => $id));
    $this->db->delete('vbc_property_amenities', array('v_ref_id' => $id));
    $this->db->delete('vbc_property_images', array('property_ref_id' => $id));
}

function delete_listing_images($images_urls) {
    unlink('uploads/property-images/'.$images_urls);
    return TRUE;
}
CodeIgniter_Learner
  • 527
  • 1
  • 6
  • 19

4 Answers4

0

Please check the permission of the folder . The folder permission should be 0777 . Also you can check this

Updated :

If your $image_urls is an array then inside your image delete function you should do

function delete_listing_images($images_urls) {
  foreach($image_urls as $image_url) //deletes all urls found
    {

   unlink('uploads/property-images/'.$image_url);




 }
return TRUE;
}
Community
  • 1
  • 1
Drudge Rajen
  • 7,584
  • 4
  • 23
  • 43
  • Hi, I am running this app on local server XAMPP (Not to sure how to check it on that)... But at the same time in my other scripts I have successfully deleted and replace user's profile picture using unlink.. I guess if that worked than this too.. The only difference is that this time I have an array of images exploded in script.. – CodeIgniter_Learner Jan 09 '16 at 09:11
  • Is $images_urls is an array ?? – Drudge Rajen Jan 09 '16 at 09:18
  • 1
    if your $images_urls is an array than you want t user foreach and set unlink() into you foreach() – jilesh Jan 09 '16 at 09:20
  • Yes $images_urls is an array paths(names) of all images in 'upload' folder.. "I mentioned that in question that I am new and not sure how to apply array for that", – CodeIgniter_Learner Jan 09 '16 at 09:23
  • Now this is the problem I am facing.. It says " Invalid argument supplied for foreach()" & "Undefined variable: image_urls" in model... I thought may be because I don't have `$images_urls['images_urls'] = $this->my_listings_model->get_property_all_images_url($id);` in controller so I changed it to that but still the same error in model. – CodeIgniter_Learner Jan 09 '16 at 09:33
  • check print_r($images_urls); in controller – msvairam Jan 09 '16 at 09:54
0

change in your controller like this :

function delete_listing_id() {
    if($this->session->userdata('is_logged_in')){
    $id = $this->uri->segment(3);
    $this->my_listings_model->delete_listing_id($id);

    $images_urls = $this->my_listings_model->get_property_all_images_url($id);
    if(!empty($images_urls))
    {
    foreach($images_urls as $image_url);
      {
      $this->my_listings_model->delete_listing_images($image_url);  
      }
    }
    $this->my_listings();
}

and set your model as it is not use foreach in it

jilesh
  • 436
  • 1
  • 3
  • 13
  • Thanks @jilesh.. error seems to be gone but when I tried to delete, It deleted rows from tables but not images... I confirmed `unlink('uploads/property-images/'.$image_url);` by opeing an image in browser that works fine, so no problem in path – CodeIgniter_Learner Jan 09 '16 at 10:39
  • your uploads folder is in root folder ? or any sub folder ? or try this unlink('/uploads/property-images/'.$image_url); i added / before uploads – jilesh Jan 09 '16 at 10:41
  • it's in root.. okay let me try this one – CodeIgniter_Learner Jan 09 '16 at 10:45
  • No.. didn't work...does that [0], [1] that I print_r($images_urls), play any role in it? – CodeIgniter_Learner Jan 09 '16 at 10:49
  • try this way unlink('/your_full_path/uploads/property-images/'.$image_url); as i.e unlink('/var/www/your_proj_name/uploads/property-images/image_name.jpeg this will work 100% – jilesh Jan 09 '16 at 10:52
  • I am sorry, doesn't work either.. Could you please check the answer in [link](http://stackoverflow.com/questions/26622735/unlink-permission-denied-on-codeigniter), and tell me what am I doing wrong? & how to make a variable of [0]? – CodeIgniter_Learner Jan 09 '16 at 11:02
  • no this is not an issue int o your code because in your array all images are there and in link there are more than one key – jilesh Jan 09 '16 at 11:04
  • you made mistake some where ? other wise it will works – jilesh Jan 09 '16 at 11:05
  • please let me know what you will get here in this function function delete_listing_images($images_urls) { unlink('uploads/property-images/'.$images_urls); return TRUE; } – jilesh Jan 09 '16 at 11:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100226/discussion-between-codeigniter-learner-and-jilesh). – CodeIgniter_Learner Jan 09 '16 at 11:09
0

check your array key and pass it in this way :

foreach($images_urls as $image_url){

   unlink('uploads/property-images/'.$image_url['your_key']);

}

If you don't have any specific key in array that is : your array is like in this way : array('0'=>'a','1'=>'b'). do in this way :

foreach($images_urls as $image_url){

   unlink('uploads/property-images/'.$image_url);

}
RK12
  • 472
  • 3
  • 11
  • I get `Array ( [0] => property_image_19.jpg [1] => property_image_110.jpg)` when I print_r(images_urls).. How do I go about passing these keys? – CodeIgniter_Learner Jan 09 '16 at 10:43
0

Finally got it working. Thanks for all of your support.

Had to replace in controller:

$images_urls = $this->my_listings_model->get_property_all_images_url($id);

by

$images_urls['images_urls'] = $this->my_listings_model->get_property_all_images_url($id);

And in model all delete_listing_images (function):

function delete_listing_images($images_urls) {
$rows = $images_urls;
if (count($rows) > 0) {
    foreach($rows as $row) {
            $number_of_images = count($row);
        }
    }
if(!empty($images_urls)) {
    for($i=0; $i<$number_of_images;$i++){
foreach($images_urls as $image_url); {
unlink('uploads/property-images/'.$image_url[$i]);
        }
    }
}
return TRUE;
}
CodeIgniter_Learner
  • 527
  • 1
  • 6
  • 19