Php 如何从类似MVC的URL确定文件类型
我想在Magento商店的产品视图页面中添加一个音频播放器,用于播放示例音频文件,因此我编辑了Php 如何从类似MVC的URL确定文件类型,php,magento,Php,Magento,我想在Magento商店的产品视图页面中添加一个音频播放器,用于播放示例音频文件,因此我编辑了Magento\u root/app/design/path/to/theme/template/downloadable/catalog/product/samples.phtml <?php if ($this->hasSamples()): ?> <dl class="item-options"> <dt><?php echo $this-
Magento\u root/app/design/path/to/theme/template/downloadable/catalog/product/samples.phtml
<?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; ?>
您可以尝试使用curl发送HEAD请求。对于HEAD请求,您只是获取标题,而不是正文(在您的情况下是音频文件):
您可以尝试使用curl发送HEAD请求。对于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>
}
<?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
)