dongsibao8977 2017-04-07 12:44
浏览 194

Symfony - 从自定义类获取根目录路径

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 simulink单相桥式整流电路
    • ¥35 问问51单片机流水灯的代码该怎么写
    • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
    • ¥15 stata webuse报错
    • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
    • ¥15 如何利用AI去除图片中的竹架子
    • ¥15 python 写个基金爬取的代码,自动卖出功能
    • ¥15 Linux系统启动不起来
    • ¥15 为什么运行仿真数码管不亮(语言-c语言)
    • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导