I declare a new class which not inherit from Controller
.
Now, I want to pull images from remote path, and store them in directory. How do I get root path?
class UserInfo
{
public function getUserAvatar($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->m_avatarOrigin); // $this->m_avatarOrigin is the remote path to pull user avatar.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, false);
$response = curl_exec($curl);
$header = '';
$body = '';
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
}
curl_close($curl);
$fileName = ROOT_PATH."/../resources/avatar/".$this->m_name.".jpg"; // I want to get "ROOT_PATH"
file_put_content($fileName, $body);
}
This class doesn't inherit from Controller
, so I cannot use the function:
$this->get('kernel')->getRootDir();
Is there any better way to let me get root directory path?