5

I am trying to remove an image from a directory using the unlink() function.

if (file_exists($oldPicture)) {
    unlink($oldPicture);
    echo 'Deleted old image';
} 
else {
    echo 'Image file does not exist';
}

The value of $oldPicture is ../images/50/56/picture.jpg.

The follow block of code runs without any errors however when I check the directory the image is still there.

Am I doing something wrong here?

Thanks

BRBT
  • 1,467
  • 8
  • 28
  • 48
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Apr 22 '15 at 17:05
  • What is the absolute path to the php file being executed. What is the absolute path to the "images/50/56/picture.jpg" path? – cwurtz Apr 22 '15 at 17:06
  • 2
    Probably that php doesn't have permission to delete the file. – Bob Nocraz Apr 22 '15 at 17:07
  • as per your edit: even if you have permissions set on the folder, doesn't mean it has for the file, so check that and use error reporting as I stated above etc. - you've been given an answer below btw. try that – Funk Forty Niner Apr 22 '15 at 17:10
  • @Fred-ii- yeah I am going to throw in that error reporting and see what it says, thanks. How would I use `var_dump()` to check folder/file permissions? – BRBT Apr 22 '15 at 17:12
  • @Fred-ii- Yeah everyone is right, I am getting denied permission...any idea how I can fix this? P.S. that error reporting is amazing, it will really help me thanks for that! – BRBT Apr 22 '15 at 17:21
  • you're welcome. give me a few minutes; am on the phone. I'll come up with a solution for you. – Funk Forty Niner Apr 22 '15 at 17:22
  • @Fred-ii- no rush, thanks so much - I owe you a fancy cappuccino. – BRBT Apr 22 '15 at 17:23
  • Yummy thanks! I posted an answer for you below. Let me know how it works out. – Funk Forty Niner Apr 22 '15 at 17:30

2 Answers2

5

"@Fred-ii- Yeah everyone is right, I am getting denied permission...any idea how I can fix this? P.S. that error reporting is amazing, it will really help me thanks for that! – BigRabbit"

Use chmod on the file before unlinking it:

if (file_exists($oldPicture)) {

// last resort setting
// chmod($oldPicture, 0777);
chmod($oldPicture, 0644);
    unlink($oldPicture);
    echo 'Deleted old image';
} 
else {
    echo 'Image file does not exist';
}

A last resort setting would be using 0777 as in the commented code.


"How would I use var_dump() to check folder/file permissions?"

  • It's not to check for permissions but to check what is being passed through that variable.

I.e.: var_dump($oldPicture);

if (file_exists($oldPicture)) {

var_dump($oldPicture);

// last resort setting
// chmod($oldPicture, 0777);
chmod($oldPicture, 0644);
    unlink($oldPicture);
    echo 'Deleted old image';
} 
else {
    echo 'Image file does not exist';
}

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Neither is working, should I assume that its not the files permissions but the directories? - I make this directory like this `mkdir($directoryPath);`. Should I be making them like `mkdir($directoryPath, 0775);`? As I am removing this image, I am also uploading another and I am also being denied permission (the error reporting is a godsend). – BRBT Apr 22 '15 at 17:50
  • @BigRabbit yes but chmod to 755 orr 777. **all** folders and sub-folders need to have proper permissions set. try and chmod 755 for the folders/sub-folders in your FTP program first, see what that says. As a "last" resort, you can set your folders to 777 but that's not safe. `mkdir($directoryPath);` does not set the folder's proper permissions. see the manual for it http://php.net/manual/en/function.mkdir.php – Funk Forty Niner Apr 22 '15 at 17:52
  • @BigRabbit Use `mkdir($directoryPath, 0775);` before you attempt in writing to it when uploading. You could also use the same method `chmod($New_Uploaded_Picture, 0644);` **after** the uploading code. I used `$New_Uploaded_Picture` as an example variable here. – Funk Forty Niner Apr 22 '15 at 17:56
  • Hmm k thanks, give me a bit and ill get back to you, just need to look over all of this and make sure I am understanding everything properly. – BRBT Apr 22 '15 at 18:00
  • Would doing `chmod($anyNewPictureOrFile, 0644);` on any newly added image or file be a good a idea, is that a thing I should be doing or ..? – BRBT Apr 22 '15 at 18:25
  • @BigRabbit yes, it's ok. Using 644 is safe and is the standard default on most servers. – Funk Forty Niner Apr 22 '15 at 18:26
  • 1
    Now that I understand `chmod()` `and mkdir()` a lot more I got it working. When I am creating new directories I am now doing `mkdir($directoryPath, 0775);`, also when I am uploading any images I run them through `chmod()`. After doing this, I was able to successfully use `unlink()` to remove images from the newly created directory. – BRBT Apr 22 '15 at 18:49
  • @BigRabbit That's great news. So I take it with can wrap 'er up and mark as solved? – Funk Forty Niner Apr 22 '15 at 18:50
  • 1
    Also I will make use of `var_dump()` more often I see the benefits it has while debugging any issues. I will be sure to remove any error reporting during production. Thanks a lot Fred, I really appreciate all of this, I have learnt a lot today, game changer. – BRBT Apr 22 '15 at 18:51
  • 1
    @BigRabbit You're welcome, glad to have been of help, *cheers* – Funk Forty Niner Apr 22 '15 at 18:51
0
function deleteDrFiles($path) {
    if (is_dir($path)) {
        array_map(function($value) {
            $this->deleteDrFiles($value);
            rmdir($value);
        },glob($path . '/*', GLOB_ONLYDIR));
        array_map('unlink', glob($path."/*"));
    }
}

Use the function like this

deleteDrFiles('../../uploads/img'); 
David Buck
  • 3,752
  • 35
  • 31
  • 35