Search code examples
phpunlink

unlink() triggers open_basedir restriction. But every other function works


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?


Solution

  • 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.