208

How do I delete a file from my server with PHP if the file is in another directory?

Here is my page layout:

  • projects/backend/removeProjectData.php (this file deletes all my entries for the database and should also delete the related file)
  • public_files/22.pdf (the place where the file is located.)

I'm using the unlink function:

unlink('../../public_files/' . $fileName);

But this always gives me an error that the file does not exist. Any ideas?

3
  • 2
    <pre> Use absolute path </pre> Commented Mar 3, 2010 at 13:09
  • 1
    if you dont check using real path, you are likely to get the "." and ".." non-files too, causing file does not exist errors Commented Feb 7, 2013 at 17:00
  • 1
    But beware, this might not really delete your file if your file has multiple file names / symlinks ! See this thread for more info: stackoverflow.com/q/17548906/1114320 Commented Jul 19, 2013 at 9:08

6 Answers 6

253

The following should help

  • realpath — Returns canonicalized absolute pathname
  • is_writable — Tells whether the filename is writable
  • unlink — Deletes a file

Run your filepath through realpath, then check if the returned path is writable and if so, unlink it.

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

12 Comments

I wonder how w3shools is doing this? w3schools.com/php/func_filesystem_delete.asp
@Gordon i am sorry.. i used the term wrong which makes a big difference .. i agree it was idiotic.. but what i tried to mean is a coding example would be more helpful .. if i need to visit three other links to understand your reply is it very helpful ? ..sorry again for the wrong word.. i am not from english speaking country ... though it can not be an excuse ...
@RitabrataGautam "if i need to visit three other links to understand your reply is it very helpful?" - Yes, it is. Because after going to the links you will have understood how it works. If I just give you the codes, you will not understand but just copy and paste. What did you learn then? Nothing. Besides, the code for that is very trivial.
@Gordon .. i agree .. your views demand respect .. you earned it #185K .. just two quick questions... 1> if i consider your reply as complete then why many moderators here say that you should provide some code also... not just links ( it cause many down-votes) ... 2> wouldn't it be better if u put some code . now who has learning tendency they will surely visit those links and who doesn't have that they will again go to google to get a ready code .
the answer seemed straight forward enough to me: but the code example you wanted: $path = realpath('../../public_files/' . $fileName); if(is_writable($path)){unlink($path);}
|
130
$files = [ './first.jpg', './second.jpg', './third.jpg' ]; foreach ($files as $file) { if (file_exists($file)) { unlink($file); } else { // File not found. } } 

1 Comment

up-voted for using the file_exists function. Otherwise you're gonna get an error if the file doesn't exist.
18

Check your permissions first of all on the file, to make sure you can a) see it from your script, and b) are able to delete it.

You can also use a path calculated from the directory you're currently running the script in, eg:

unlink(dirname(__FILE__) . "/../../public_files/" . $filename); 

(in PHP 5.3 I believe you can use the __DIR__ constant instead of dirname() but I've not used it myself yet)

1 Comment

I checked the permissions and I wasn't able to see the file at first but now everything works thanks to the realpath. thanks for the advice
12

You can delete the file using

unlink($Your_file_path); 

but if you are deleting a file from it's http path then this unlink is not work proper. You have to give a file path correct.

Comments

7

AIO solution, handles everything, It's not my work but I just improved myself. Enjoy!

/** * Unlink a file, which handles symlinks. * @see https://github.com/luyadev/luya/blob/master/core/helpers/FileHelper.php * @param string $filename The file path to the file to delete. * @return boolean Whether the file has been removed or not. */ function unlinkFile ( $filename ) { // try to force symlinks if ( is_link ($filename) ) { $sym = @readlink ($filename); if ( $sym ) { return is_writable ($filename) && @unlink ($filename); } } // try to use real path if ( realpath ($filename) && realpath ($filename) !== $filename ) { return is_writable ($filename) && @unlink (realpath ($filename)); } // default unlink return is_writable ($filename) && @unlink ($filename); } 

3 Comments

looks perfect, thanks a lot but please update it since you can not use unlink as name for your function.
If its a public static function in a class, unlink is a valid function name: 3v4l.org/MgA2l
Yeah, but in the current scenario, Standalone is a better choice.
1

I know this question is a bit old, but this is something simple that works for me very well to delete images off my project I'm working on.

unlink(dirname(__FILE__) . "/img/tasks/" . 'image.jpg'); 

The dirname(__FILE__) section prints out the base path to your project. The /img/tasks/ are two folders down from my base path. And finally, there's my image I want to delete which you can make into anything you need to.

With this I have not had any problem getting to my files on my server and deleting them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.