0

I've been working with my codeigniter project for gallery part.

I've succeed on displaying all images, now I want to make function to delete images from folder. I tried it but get some error.

My Controllers

function delete_img(){
    if (array_key_exists('delete_file', $_POST)) {
      $filename = $_POST['delete_file'];
      if (file_exists($filename)) {
        unlink($filename);
        echo 'File '.$filename.' has been deleted';
      } else {
        echo 'Could not delete '.$filename.', file does not exist';
      }
    }
}

My Full Views, Gallery Part

<?php
    //FOLDER GALLERY-PART
     $dirname = "./assets/images/gallery/";
     $images = glob($dirname."*.jpg");

     array_multisort(array_map('filemtime', $images), SORT_NUMERIC, SORT_DESC, $images);

     foreach($images as $image) {

    "<div class='col-xs-6 col-sm-4 col-md-3 portfolio-item logos'>
       <div class='portfolio-wrapper'>
         <div class='portfolio-single'>
           <div class='portfolio-thumb'>
             <img src='.$image.' class='img-responsive' alt=''>
               </div>
                <div class='portfolio-view'>
                   <ul class='nav nav-pills'>
                     <li><a href='gallerydetail'><i class='fa fa-link'></i></a></li>
                     <li><a href='.$image.' data-lightbox='example-set'><i class='fa fa-eye'></i></a></li>
                     <li><a href='#'><i class='fa fa-heart'></i></a></li>
                        </ul>
                </div>
              </div>
              <div class='portfolio-info'>
                    <form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
                        <input type = 'hidden' name='delete_file' value='.$image.' />
                        <input type = 'submit' value='Delete' class='btn btn-common'/>
                    </form>
                </div>
            </div>
        </div>";
            }
        ?>

I get this code from here I tried to implement this into codeigniter, the result was error. Like this:

Could not delete ../assets/images/gallery/hermesed231486375164.jpg., file does not exist

The file was exist, but idk why it errors.

EDITED: Ok I was trying to fixed it with base_url, the result:

Could not delete http://k-art.local/../assets/images/gallery/hermesed231486375164.jpg., file does not exist

Now, how to delete this thing [../] it might worked when it disappear

7
  • Are you working in live or in localhost? Commented Feb 6, 2017 at 9:37
  • try to print $filename and make sure that this file is really exists in your directory, I see a point after the file extension hermesed231486375164.jpg*.*, from where this point came? Commented Feb 6, 2017 at 9:38
  • There's some mistakes over there ../assets/images/gallery/blabla.jpg How to make it work with base_url? Commented Feb 6, 2017 at 9:44
  • check $filename value using print_r($filename); what it returns Commented Feb 6, 2017 at 9:44
  • @Mohammad that name come from my username session + date('U') at upload part. It's the name of the image that I want to delete Commented Feb 6, 2017 at 9:48

2 Answers 2

1

SOLVED WITH IDIOT WAY

From this:

<input type = 'text' name='delete_file' value='.$image.' />

To this.. Just deleting the dot[.] mark. Really? LMAO

<input type = 'text' name='delete_file' value='$image' />

But anyway thanks for replying :)

Sign up to request clarification or add additional context in comments.

1 Comment

as I told you early, pay attention to the point :-P, next time try to share your whole "related to the issue" code
0

Change permission folder gallery for 0777

OR Try:

function delete_img(){
    if (array_key_exists('delete_file', $_POST)) {
      $filename = $_POST['delete_file'];
      if (file_exists($filename)) {
        chmod($filename, 0777);
        if(unlink($filename)) {
            echo 'File '.$filename.' has been deleted';    
        }else echo 'Could not delete '.$filename;
      }else echo 'File does not exist: '.$filename;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.