Symfony2 - checking if file exists

前端 未结 6 1259
执笔经年
执笔经年 2020-12-05 05:59

I have a loop in Twig template, which returns multiple values. Most important - an ID of my entry. When I didn\'t use any framework nor template engine, I used simply

6条回答
  •  孤城傲影
    2020-12-05 06:23

    Improving on Sybio's answer, Twig_simple_function did not exist for my version and nothing here works for external images for example. So my File extension file is like this:

    namespace AppBundle\Twig\Extension;
    
    class FileExtension extends \Twig_Extension
    {
    /**
     * {@inheritdoc}
     */
    
    public function getName()
    {
        return 'file';
    }
    
    public function getFunctions()
    {
        return array(
            new \Twig_Function('checkUrl', array($this, 'checkUrl')),
        );
    }
    
    public function checkUrl($url)
    {
        $headers=get_headers($url);
        return stripos($headers[0], "200 OK")?true:false;
    }
    

提交回复
热议问题