建立Captcha wav时,fseek,feof和fread的PHP警告消息

时间:2012-10-10 02:15:34

标签: php streaming wav fread fseek

当我尝试运行fseek,feof和fread时,我正在收到PHP警告消息(“期望参数1为资源”)。我相信这是因为它无法在每种情况下找到该文件。

让我给你一点背景。我正在构建验证码wav然后流式传输文件。它适用于所有现代浏览器(除了IE,但我用一些Flash覆盖它),但在iOS中速度很慢。

我认为问题出在$ fm变量上,但任何帮助都会受到赞赏。

public function outputAudioFile()
{
    set_error_handler(array(&$this, 'errorHandler'));

    require_once 'wavfile.php';

    try {
        $audio = $this->getAudibleCode();
    } catch (Exception $ex) {
        if (($fp = @fopen(dirname(__FILE__) . '/si.error_log', 'a+')) !== false) {
            fwrite($fp, date('Y-m-d H:i:s') . ': secureaudio audio error "' . $ex->getMessage() . '"' . "\n");
            fclose($fp);
        }

        $audio = $this->audioError();
    }

    if ($this->canSendHeaders() || $this->send_headers == false) 
    {
        if ($this->send_headers) 
        {
            //download range
            $file_size   = strlen($audio);
            $start_point = 0;
            $end_point   = $file_size - 1;

            if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])) 
            {                       
                //download range
                $http_range = explode('-', substr($_SERVER['HTTP_RANGE'], strlen('bytes=')));
                $start_point = $http_range[0];
                if($http_range[1] > 0)
                    $end_point = $http_range[1];

                //headers
                header('HTTP/1.1 206 Partial Content');
                header('Status: 206 Partial Content');
                header('Content-Length: '.($end_point - $start_point + 1));
            }
            //full download
            else
            {
                //headers
                header('HTTP/1.1 200 OK');
                header("Status: 200 OK");
                header('Content-Length: '.$file_size);
            }

            //headers
            $uniq = md5(uniqid(microtime()));
            header('Content-Type: audio/x-wav');
            header('Accept-Ranges: bytes');
            header("Content-Range: bytes $start_point-$end_point/$file_size");
            header("Content-Disposition: attachment; filename=\"secureaudio-{$uniq}.wav\"");
            header("Last-Modified: ".gmdate("D, d M Y H:i:s", time() + 3600*(0+date("I")))." GMT");
            header("Expires: ".gmdate("D, d M Y H:i:s", time() + 7200*(0+date("I")))." GMT");
            header('Cache-Control: no-store, no-cache, must-revalidate');
            header('Pragma: no-cache');
            header("Content-Transfer-Encoding: binary\n");
            header('Connection: close');               

            $fm     = fopen($audio, 'r');
            fseek($fm,$start_point);

            while(!feof($fm)&&$start_point<$end_point&&(connection_status()==0))
            { 
                print fread($fm,min(1024*16,$end_point-$start_point));
                $start_point+=1024*16;
            }
        }    
        //output it
        echo $audio;
    }
    else 
    {
        echo '<hr /><strong>'
            .'Failed to generate audio file, content has already been '
            .'output.<br />This is most likely due to misconfiguration or '
            .'a PHP error was sent to the browser.</strong>';
    }

    restore_error_handler();

    if (!$this->no_exit) exit;
}

1 个答案:

答案 0 :(得分:0)

  1. 从fopen中删除“@”
  2. 确保将错误报告设置为E_ALL

    的error_reporting(E_ALL);

  3. 首先找到错误,然后修复它。这应该删除其余的警告。