文件不存在或未找到。

4

我正在项目中实现验证码功能。我使用的是CodeIgniter框架。在captcha_helper.php文件中,代码如下:

$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;

返回了 false。因为 file_exists($font_path) 返回了 false。 我传递的参数是

$img_path = '../../captcha/';
        $img_url = base_url() . 'captcha/';
        $font_path = base_url(). 'captcha/font/impact.ttf';

$captch = array(
            'word' => '',
            'img_path' => $img_path,
            'img_url' => $img_url,
            'font_path' => $font_path,
            'img_width' => '200',
            'img_height' => '50',
            'expiration' => '300'
        );
 $cap = create_captcha($captch);

现在,当我打印出$font_path并将相同的网址复制粘贴到浏览器中时,字体文件会提示下载,这意味着路径是正确的。那么,为什么file_exists($font_path)返回false呢?我尽力了也想不明白。请帮帮我,如果有任何错误,请原谅我愚蠢的错误。
2个回答

2
根据PHP手册,file_exists函数接受文件名作为参数:文件或目录的路径,而不是URL。它不使用HTTP请求访问文件。我敢打赌file_exists('/captcha/font/impact.ttf')会返回true!

2

尝试:

$font_path = realpath($image_path . 'font/impact.ttf');

问题在于base_url()返回的是一个URL,而你需要使用完整路径来查找本地文件系统(例如:/home/yoursite/public_html/captcha/font/impact.ttf)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接