CakePHP:使用Flash播放器流式传输的mp3文件的媒体视图

时间:2011-09-16 14:46:41

标签: file cakephp views streaming media

我有一个带有音频flash播放器的页面,它可以传输几个mp3文件。此播放器使用的XML文件包含所有这些文件的路径,这些文件位于webroot目录的 audio 文件夹中。

我不喜欢的是,用户很容易找到XML文件(也在webroot目录中)并查看mp3文件的路径,然后下载它们而不是仅使用flash player。

我已经看到CakePHP有一个名为媒体视图的东西,你可以在webroot外面设置一个文件夹并将文件放在那里,这样用户就无法自由访问它们。 Cake的网站(http://book.cakephp.org/view/1094/Media-Views)中的解释主要集中在要下载的文件上,但我想知道我是否可以使用媒体视图来传输我的mp3文件音频播放器无需用户下载。或者,如果还有其他没有媒体视图可供选择的替代方案:)

提前非常感谢!

澄清

这是我想要改变的代码:

- 在我的控制器中:

function index() {}

- 在我看来(index.ctp):

<embed type="application/x-shockwave-flash" flashvars="audioUrl=/audios/01 Track 01.mp3" src="audio-player.swf"></embed>

=&gt;如何使用媒体视图更改它,以便我的mp3文件位于app / audios文件夹而不是app / webroot / audios?

这就是我所做的:

- 在我的控制器(streams_controller.php)中:

function index() {}

function getFile() {

    $this->view = 'Media';

    $params = array(
                    'id' => '01 Track 01.mp3',
                    'name' => '01 Track 01',
                    'download' => false,
                    'extension' => 'mp3',
                    'path' => APP . 'audios' . DS,
                    'cache' => true
                    );

    $this->set($params);
}

- 在我看来(index.ctp):

<embed type="application/x-shockwave-flash" flashvars="audioUrl=/streams/getFile" src="audio-player.swf"></embed>

仍然没有运气!我做错了什么?

3 个答案:

答案 0 :(得分:1)

Media视图似乎就是您需要的。它将允许您将mp3移动到webroot之外。 只有当你为选项中的'download'键传递true时,Cake才会发送标题以强制下载。

将if设置为false,它可能会起作用。

答案 1 :(得分:0)

我认为,默认情况下,用户可以从webroot文件路径或/ controller / action路径下载文件。

但是这很容易克服,有几个安全实现。

注意:在我看来,如果你使用某种授权,你应该根据他们的['用户'] ['id']而不是他们的IP进行哈希

/**
* Action to download special assets
* example link to download <?php echo $this->Html->link('Link Text', array('action' => 'download', Security::hash('filedownloadsalt'.date('d').env('REMOTE_ADDR')), $filename), array('escape' => false)); ?>
* example URL to download <?php echo $this->Html->url(array('action' => 'download', Security::hash('filedownloadsalt'.date('d').env('REMOTE_ADDR')), $filename)); ?>
* @param string $hash
* @param string $filename
*/
function download($hash=null, $filename=null) {
    // check to ensure that the input hash is specific to this user and created today
    if (Security::hash('filedownloadsalt'.date('d').env('REMOTE_ADDR'))!=$hash) {
        $this->Session->setFlash("Sorry, you are not allowed access to this asset");
        return $this->redirect(array('action' => 'failure'));
    }
    // check to ensure referrer URL is on this domain
    if (strpos(env('HTTP_REFERER'), env('HTTP_HOST'))===false) {
        $this->Session->setFlash("Sorry, you must access this asset from within my site, not directly");
        return $this->redirect(array('action' => 'failure'));
    }
    // now get the asset
    $filenameParts = explode('.', $filename);
    $filenameExt = array_pop($filenameParts);
    $filenameBase = implode('.', $filenameParts);
    $this->view = 'Media';
    $params = array(
        'id' => $filename,
        'name' => $filenameBase,
        'download' => true,
        'extension' => strtolower($filenameExt),
        'path' => APP . 'files' . DS,   // don't forget terminal 'DS'
        'cache' => true,
        );
    $this->set($params);
}

答案 2 :(得分:0)

Cakephp 3x媒体文件闹剧下载

if($ this-&gt; request-&gt;是(['post'])){      $ data = $ this-&gt; request-&gt; data;

 // $file_name= 'test.mp4';
 $file_name = $data['files'];
 $this->viewClass = 'Media';

  $file_path = WWW_ROOT . 'files/video/' . DS . $file_name;
 //showing the path to the server where the file is to be download
 $this->response->file($file_path, array(
    'download' => true,
    'name' => $file_name,
 ));
 return $this->response;

 exit;
}