0

I have the following constant: FSROOT that is set as follows: getcwd(). I can use this constant everywhere in my app and don't have to worry about paths, for example: require_once(FSROOT . '/includes/php/something.php).

However, I have recently implemented some files in my app, and on certain events I need to delete some files, here's what I do locally:

unlink( FSROOT . '/somefile.pdf' );

And this works perfectly, in my local environment. On the server however I get an error:

unlink(): open_basedir restriction in effect. File(/mnt/var/[...]/app/somefile.pdf) is not within the allowed path(s): (/var/[...]/app/)

So the problem seems to be the /mnt/ directory that is returned from getcwd() but that doesn't match my allowed paths!

I am a bit confused because the FSROOT path can be used in any function but unlink(). This is also why I don't want to change it. Is there another way to delete the file? Or will I have to manually create another constant to be used with unlink()?

Or is there an alternative to getcwd() that I should use?

Louis B.
  • 2,348
  • 1
  • 29
  • 51
  • if you are trying to delete any file that is outside of open_basedir configuration this error will be triggered. You can check this config in php.ini. – Leri Jan 04 '13 at 13:48
  • If you're using PHP >= 5.3 you could also turn SafeMode off, which is deprecated in 5.3, then there'll be no more issues with `open_basedir`... – Havelock Jan 04 '13 at 13:53
  • PLB: Thanks, so I am looking for ways around this! Havelock: I can't do this as I am on a shared host. :( – Louis B. Jan 04 '13 at 15:03
  • Turns out I was storing the file's path in a non-persistent way, and my host sometimes moves my app around on the server, so the path would change, and the unlink() would try to access a non-existing path... – Louis B. Jan 06 '13 at 11:40

2 Answers2

0

Have you tried using dirname in combination with FILE to get the correct path?

dirname(__FILE__)
tlenss
  • 2,609
  • 2
  • 22
  • 26
0

Turns out I was storing the file's path in a non-persistent way, and my host sometimes moves my app around on the server, so the path would change, and the unlink() would try to access a non-existing path... So there really isn't anything to be answered here, sorry.

Louis B.
  • 2,348
  • 1
  • 29
  • 51