2011-09-16 42 views
1

我有一个带有音频Flash播放器的页面,可以传输多个mp3文件。此播放器使用包含所有这些文件路径的XML文件,这些文件位于webroot目录中的音频文件夹中。CakePHP:用Flash播放器流式传输的MP3文件的媒体视图

我不喜欢的是,用户到达XML文件(它也位于webroot目录中)并查看mp3文件的路径非常简单,然后下载它们而不是仅仅使用Flash播放器。

我见过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> 

=>如何更改媒体视图,以便我的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> 

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

回答

1

媒体视图似乎是你需要在这里。它可以让你在webroot之外移动mp3。 只有当您在选项中为“下载”键传递true时,Cake才会发送标题以强制下载。

只要设置为false,它可能会工作。

+0

我已经尝试在下载键中显示'false',但下载对话框仍然出现,只是要求下载页面而不是文件。 – Albert

0

我想,默认情况下,用户将能够从webroot文件路径或从/ controller/action路径下载文件。

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

注:在我看来,如果你正在使用某种形式的授权,你应该基于哈希他们[“用户”] [“身份证”],而不是他们的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); 
} 
+0

感谢您的代码,但它看起来像它的作品,如果我想将文件放在一个文件夹下载。我只想让Flash播放器在另一个文件夹中找到它们... – Albert

0

CakePHP的3X媒体文件闹剧下载

if($ this-> request-> is(['post'])){ $ data = $ this-> request-> 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; 
} 
相关问题