doudou3213 2012-10-10 02:15
浏览 50

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

I'm currently getting PHP Warning messages saying ("expects parameter 1 to be resource") when try to run fseek, feof and fread. I believe this is because it can't find the file in each case.

Let me give you a little background. I'm building a captcha wav and then streaming the file. It working in all modern browsers (except IE, but I'm covering this with some Flash), but is slow in iOS.

I believe the problem is with the $fm variable, but any help would be appreciated.

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() . '"' . "
");
            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
");
            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条回答 默认 最新

  • douxin8610 2012-10-10 02:32
    关注
    1. Remove the "@" from the fopen
    2. Make sure that you have error reporting set to E_ALL

      error_reporting(E_ALL);

    Find the error first, and then fix it. That should remove the rest of the warnings.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题