我声明了一个不从Controller
继承的新类。
现在,我想从远程路径中提取图像,并将它们存储在目录中。我如何获得根路径?
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);
}
此课程不会从Controller
继承,因此我无法使用该功能:
$this->get('kernel')->getRootDir();
有没有更好的方法让我获得根目录路径?