1

我目前在尝试运行 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;
}
4

1 回答 1

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

    错误报告(E_ALL);

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

于 2012-10-10T02:32:59.590 回答