2011-09-16 45 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; 
} 
相關問題