文件不存在或未找到

File does not exits or found

我正在项目中实施验证码。我正在使用 codeigniter 框架。在 captcha_helper.php 文件中代码

$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : 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并在浏览器中复制粘贴相同的url时,提示字体文件正在下载,说明路径正确。那么,为什么 file_exists($font_path) 返回 false。 我尽了最大努力无法弄清楚。请帮助我,如果有任何愚蠢的错误,请原谅我。

根据 PHP manual,file_exists 接受文件名作为其参数:文件或目录的路径,而不是 URL。它不使用 HTTP 请求访问文件。我打赌 file_exists('/captcha/font/impact.ttf') 会 return 正确!

尝试:

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

问题是 base_url() returns a URL 并且您需要使用完整路径查看本地文件系统(例如 /home/yoursite/public_html/captcha/font/impact.ttf