Search code examples
phpdirectoryrequireunlink

PHP - unlink() works with direct path but not a required one


I am using unlink() to delete a PDF file from a directory. In this case, the name of the file is stored in a variable i.e. $fileName. I have tested to make sure the file name in the variable matches the one in the directory.

For some reason it is working with a direct path but not a required one. For example:

working:

unlink('C:/xampp/htdocs/wordpress/wp-content/uploads/sample_dir/' . $fileName);

not working:

unlink(require_once __DIR__ . '/../../uploads/sample_dir/' . $fileName);

There are no errors thrown, it just does nothing. Oof.


Solution

  • I noticed you're using Wordpress so just use wp_upload_dir().