建立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. 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.