1

My script is in http://localhost/path/test/index.php and the file I want to delete is in http://localhost/path/media/test.txt.

I want to have the path of the project as a constant PATH which would be path/ in this example. So I tried it with the root-relative path unlink("/" . PATH . "media/test.txt"), which didn't work.

Any ideas how to solve this path problem?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Max Meuten
  • 160
  • 1
  • 7
  • Does the directory structure on disk match the URL structure? If so: `unlink('../media/text.txt')` – George Cummins Aug 25 '14 at 21:44
  • 1
    `"/" . PATH . "media/test.txt"` is not root-relative, at least if by "root" you mean "web server document root". That path is as absolute (as in, filesystem absolute) as it gets. – Jon Aug 25 '14 at 21:46
  • web urls have very little to do with file system paths. you cannot use one in place of the other. – Marc B Aug 25 '14 at 22:25

1 Answers1

2

By putting a / at the begining of the unlink, you are telling PHP to dlete from the root of the server's file system, which is unlikely to be the same folder as your localhost (probably /var/www/)

Ideally in web applications, you should define the root of your application in the filesystem, e.g.:

$root = '/var/www/sites/project/';

Then you can unlink like:

unlink( $root . "media/test.txt" );

Alternatively you can unlink by relative, rather than absolute path (as above:)

unlink( '../media/test.txt' );

To get your root, see: this

Community
  • 1
  • 1
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48