tags:

views:

84

answers:

3

Hi,

how do I delete a file e.g 22.pdf from my server with php if the file is in an other directory

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)

now I'm using the unlink('../../public_files/' . $fileName);

but this allways gives me an error that the file does not exist any ideas?

regards, Ken

A: 
 

Use absolute path  

pavun_cool
+2  A: 

The following should help

  • realpath — Returns canonicalized absolute pathname
  • is_readable — Tells whether a file exists and is readable
  • unlink — Deletes a file

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

Gordon
Thank you very much worked like a charm :-)
Ken
+1  A: 

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)

richsage
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
Ken