'php write file path from two different location

I have a php class used to manage write/read/delete files. The constructor receives the path (string) of the file. Basically something like:

class customFile{
    public function __construct($path){
        $this->path=$path;
    }

    public function write($content){
        //use fopen and fwrite to write content
    }
    public function read(){
        //use fopen and fgets to read content
    }
    public function delete(){
        //use unlink to delete the file
    }
}

Also I have this directory tree

├─ root/
│  ├─ helpers/
│  │  ├─ customFile.php
│  ├─ dir1/
│  │  ├─ function1.php
│  ├─ tempDir/
│  ├─ function2.php
│  ├─ useCustomFileFunction.php

Is very important to say that useCustomFileFunction.php is in charge of writing the files in the tempDir, so that function create a customFile object with a path like this "./tempDir/"+someName (for example "./tempDir/writtenFile.txt).

both function1.php and function2.php call useCustomFileFunction with some file name (to write some files) but when the function is called from function1.php the result is (incorrect):

├─ root/
│  ├─ helpers/
│  │  ├─ customFile.php
│  ├─ dir1/
│  │  ├─ function1.php
│  │  ├─ tempDir/
│  │  │  ├─ writtenFile.txt
│  ├─ tempDir/
│  ├─ function2.php
│  ├─ useCustomFileFunction.php

and when is called from function2.php, the result is (correct):

├─ root/
│  ├─ helpers/
│  │  ├─ customFile.php
│  ├─ dir1/
│  │  ├─ function1.php
│  ├─ tempDir/
│  │  ├─ writtenFile.txt
│  ├─ function2.php
│  ├─ useCustomFileFunction.php

So the question is: Is there a way to tell fwrite, that the path begin from the class/that call fwrite? (customFile1.php or useCustomFileFunction.php instead of function1.php and function2.php)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source