2016-03-31 16 views
0

サンプルオーディオファイルを再生するためのMagentoストアの製品ビューページにオーディオプレーヤーを追加したいので、magento_root/app/design/path/to/theme/template/downloadable/catalog/product/samples.phtmlを編集しました。MVCのようなURLからファイルの種類を判断する方法

<?php if ($this->hasSamples()): ?> 
<dl class="item-options"> 
    <dt><?php echo $this->getSamplesTitle() ?></dt> 
    <?php $_samples = $this->getSamples() ?> 
    <?php foreach ($_samples as $_sample): ?> 
     <dd> 
      <!--HTML5 Audio player--> 
      <audio controls> 
       <source src="<?php echo $this->getSampleUrl($_sample) ?>" type="audio/mpeg"> 
       Your browser does not support the audio element. 
      </audio> 
      <br/> 
      <a href="<?php echo $this->getSampleUrl($_sample) ?>" <?php echo $this->getIsOpenInNewWindow() ? 'onclick="this.target=\'_blank\'"' : ''; ?>><?php echo $this->escapeHtml($_sample->getTitle()); ?></a> 
     </dd> 
    <?php endforeach; ?> 
    </dl> 
<?php endif; ?> 

これはうまくいきますが、プレーヤーにはオーディオファイルのみを表示します。私の問題は、$this->getSampleUrl($_sample)によって返されたURLの形式が http://example.com/index.php/downloadable/download/sample/sample_id/1/で、URLのファイルタイプに関する情報がないことです。

ファイルの種類を判断するためにURLの内容を取得することを検討しましたが、ファイルの種類を判断するためにファイルを完全に読み取るのは愚かであると思います。 試しましたpathinfo()ただし、ファイルの種類について何も返しません。

私はあなたがカールしてHEADリクエストを送信してみてくださいすることができ、この

$sample_file = $this->getSampleUrl($_sample); 
$type = getFileType($sample_file); 
if(preg_match('audio-file-type-pattern',$type){ ?> 
<!--HTML5 Audio player--> 
<audio controls> 
    <source src="<?php echo $sample_file ?>" type="<?php echo $type?>"> 
    Your browser does not support the audio element. 
</audio> 
} 

答えて

2

ような何かを達成したいです。 HEADリクエストを使用すると、ちょうど(あなたの場合、オーディオファイルに)ヘッダとない身体を得ている:あなたは、ファイルのコンテンツタイプを取得することができます単純な正規表現で

<?php 

$url = 'http://domain.com/index.php/downloadable/download/sample/sample_id/1/'; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20); 

// Only calling the head 
curl_setopt($ch, CURLOPT_HEADER, true); // header will be at output 
curl_setopt($ch, CURLOPT_NOBODY, true); 


$content = curl_exec ($ch); 
curl_close ($ch); 


echo $content; 

//Outputs: 
HTTP/1.1 200 OK 
Date: Fri, 01 Apr 2016 16:56:42 GMT 
Server: Apache/2.4.12 
Last-Modified: Wed, 07 Oct 2015 18:23:27 GMT 
ETag: "8d416d3-8b77a-52187d7bc49d1" 
Accept-Ranges: bytes 
Content-Length: 571258 
Content-Type: audio/mpeg 

preg_match('/Content\-Type: ([\w\/]+)/', $content, $m); 

echo print_r($m,1); 

//Outputs: 
Array 
(
    [0] => Content-Type: audio/mpeg 
    [1] => audio/mpeg 
) 
関連する問題