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?
The following should help
realpath
— Returns canonicalized absolute pathnameis_writable
— Tells whether the filename is writableunlink
— Deletes a fileRun your filepath through realpath, then check if the returned path is writable and if so, unlink it.