0

I'd like to specify a path relative to $HOME directory of a user. For example:

$path = "~/deep/down/in/folder.zip";
$size = filesize($versionPath)

returns an error

filesize(): stat failed for ~/deep/down/in/folder.zip

If I use absolute path instead

$path = "/home/notapacherootfolder/deep/down/in/folder.zip";
$size = filesize($path);

it work as expected. Is it possible to use ~ in file names with PHP?

sanjihan
  • 5,592
  • 11
  • 54
  • 119
  • there is no meaning for `~` when used in file path in PHP, usually `__FILE__` and [realpath](https://www.php.net/realpath) are used for files paths. – ROOT Feb 10 '20 at 17:57
  • As far as I know Linux shells replace `~` with current user's home directory, just like they replace `*`. I don't think there's a similar feature in PHP, let alone in web environment (not clear if that's the case). – Álvaro González Feb 10 '20 at 18:00
  • You could try `./` instead ( of `~/` ) if you want relative path – Toni Michel Caubet Feb 10 '20 at 18:00
  • @ToniMichelCaubet Working directory and home directory do not necessarily match. – Álvaro González Feb 10 '20 at 18:01
  • 1
    To get user home you can use [getenv](http://docs.php.net/manual/en/function.getenv.php) like so `getenv("HOME")`, – ROOT Feb 10 '20 at 18:01

0 Answers0