play media file or open image in browser cakephp - cakephp

i am using cakephp 2.x i want to play the audio and open image in a browser ....
In my 'view' I have the following code which is correctly showing the filename, and displaying the download link and the file is successfully downloading
<?php echo $this->Html->link('Download', array('controller' => 'bugshot', 'action' => 'download', $files['Audio']['filename']));?>
now i want to play this audio file as well ..as i have two licks on the page ,,, first is download and tthe other is view or play
In my controller I have the following code that is downloading the file
public function download($filename) {
$idUser = $this->Auth->user('idUser');
$folder_url = APP.'uploads/'.$idUser.'/'.$filename;
$this->response->file($folder_url, array('download' => true, 'name' => $filename));
return $this->response;
}

Downloading and serving files is almost the same
Serving files via Cake can be done with one function similiar to the following:
public function download($filename) {
$download = !empty($_GET['download']); // <- example
$idUser = $this->Auth->user('idUser');
$folder_url = APP.'uploads/'.$idUser.'/'.$filename;
$this->response->file($folder_url, array('download' => $download, 'name' => $filename));
return $this->response;
}
That way requesting the url /.../this-file.mp3 will serve the file, whereas the url /.../this-file.mp3?dowload=1 will download it.
Audio is a html5 tag
The simplest way to serve audio, is to just use the audio tag:
<?php
$url = Router::url(array('controller' => 'x', 'action' => 'download', $name));
$download = Router::url(array('controller' => 'x', 'action' => 'download', $name, '?' => array('download' => 1)));
?>
<audio src="<?= $url; ?>" controls>
<!-- alternate content for unsupported case -->
Download Download it;
</audio>
Being a relatively new tag, support isn't universal - see Detailed article on support or other resources for more information on how to handle browsers that do not support this tag.
Or, use one of the many flash based media players that exist.

Related

In Yii2, I need to download the file that I uploaded in /web/uploads/ folder. How do i do it?

Here is the the part of the view, I made a download button.
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Download', ['download', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
Here is the download function on my controller, I cant get it to work.
public function actionDownload($id)
{
$model = new Items();
$path = Yii::getAlias('#webroot');
$file = $path . '/' .$model->item_pathname;
if (file_exists($file)) {
Yii::$app->response->sendFile($file);
}
}
The path of the file is saved in the database's item_pathname (e.g. "/uploads/samplefile.doc". I don't know how to successfully access it to append it to the path variable. It would be of great help to solve this for me. Thanks!
If a download takes too much time, I see 2 possibilities
You can increase the max execution time of your script. That's not the best solution as the script will still time out for too big files but that's the simplest solution (there may be performance considerations unrelated to your question). To do so :
ini_set('max_execution_time', 5*60); // 5 minutes
You can use the X-SendFile header of Apache (if this module in enabled in Apache only) to let Apache handle the sending of the file. More about this on Yii2 documentation http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-files. Beware of bugs in IE<=8.
if (file_exists($file)) {
Yii::$app->response->xSendFile($file);
}

CakePHP Clicking PDF Link and View it on New Tab

Ok, so I have a web app that uploads a file to the webserver. My input fields in my upload form include: type of upload (dropdown list), title, description, and the file to be uploaded, which is a PDF.
Once the PDF file is uploaded, the download link will appear in another page for the public to see. In addition, the title typed in the input field is the download link.
Now, I want to change my code. Instead of downloading it directly when the link is clicked I want it to open in a new tab, so the users can first look at the PDF file then download it from there.
Here are my codes.
Controller:
public function sendFile(){
$id = $this->request->params['pass'][0];
$staffup = $this->StaffUpload->find('first', array('conditions' => array('iduploads'=>$id)));
$this->response->file($staffup['StaffUpload']['dest'], array('download' => true, 'name' => $staffup['StaffUpload']['title']));
return $this->response;
}
The code above is the download function.
public function resources() {
$this->layout = 'website';
$this->set('staff_uploads', $this->StaffUpload->find('all', array('conditions' => array('type' => 'Resource'))));
}
The code above is the view wherein I show all uploaded files which type is Resources.
View:
<?php
foreach ($staff_uploads as $staff_uploads) {
?>
ul>
<li>
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array('controller' => 'websites', 'action' => 'sendFile', $staff_uploads['StaffUpload']['iduploads']));
?>
</li>
</ul>
<?php
}
?>
The code above shows the view.
So yeah, back to the question. I want to change the download link to a link in which when clicked, will show the PDF file in a new tab. How do I do that? And by the way, the codes posted above are all working properly. I just want to change my code so that it will be viewed in a new tab when clicked.
Thank you!
According to the docs:
echo $this->Html->link(
'Enter',
'/pages/home',
array('target' => '_blank')
);

CakePHP Open to New Tab on Click

I have a function in my application which the users can upload files to the webserver. Then these uploaded files will appear in another page wherein another type of users can click on the link. Once the link is clicked, a new tab will open and the file will be shown.
But I can't seem to do it. Using the 'target' => '_blank' is not working, or I may have put it on the wrong part of the code.
In my case, when you click on the link, the file will load on the same tab.
Here's my code:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
'target' => '_blank',
$staff_uploads['StaffUpload']['iduploads']
)
);
?>
Thank you in advance!
The correct code is:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
$staff_uploads['StaffUpload']['iduploads']
), array('target' => '_blank')
);
?>
And do read the documentation as burzum has suggested.
Read the documentation.
HTML attribute options go into the 3rd argument of the link() method, not the second which is the URL as string or array.
Problems like this can be simply resolved by using the documentation.

Render view to variable in CakePHP 1.3 (to generate pdf and download file)

I'm trying to render a view to a variable.
This variable will then be used to generate a pdf.
Then that pdf should be downloaded with the Media view.
Here's my controller code:
$dir = ROOT . '/app/tmp/evaluationpdf/';
$path = $dir . $evaluationid . '.pdf';
$evaluation = $this->SelfEvaluation->find('first', array(
'conditions' => array('SelfEvaluation.id' => $evaluationid),
'contain' => array('Submission' => array('Application'), 'Applicant', 'Member')));
$this->set(compact('evaluation'));
$this->output = '';
$this->layout = false;
$html = $this->render('/elements/self_evaluation_pdf');
$this->_generate_pdf($html, $path);
$this->view = 'Media';
$params = array(
'id' => $evaluationid . '.pdf',
'name' => $evaluationid,
'download' => true,
'extension' => 'pdf',
'path' => $dir,
);
$this->set($params);
The file is created as it should, but the first '$this->render' output is also sent to the browser.
The file is never downloaded.
Any idea on how to fix this?
The simple fix is to just set $this->output to '' after your first render() call.
The more correct way is to use requestAction() instead of render().
In CakePHP 2.x I did the following in order to use a view to generate a barcode label format:
$response = $this->render('/Labels/' . $printer['Printer']['model'] . '/manifest', 'ajax');
$body = $response->body();
$response->body('');
Which gave me the view data as $body. Then I could just redirect or if the request was ajax just set autoRender to false and return ''.
It kind of muddies the MVC waters but it is simple.
You just have to write the code below in self_evaluation_pdf.ctp
header("Content-Disposition: attachment; filename='downloaded.pdf'");
header('Content-type: application/pdf');
The dynamic content in this view will be downloaded as a PDF file on the client side.

CakePHP page with no headers/footers

In a download page for a blob from a database, how would I make it so that no other output is sent? Right now it's sending the header, debug info, and a footer. How do I make it so that none of that is sent, just for that view?
you can create an clear layout (e.g. empty.ctp ) in you layouts folder, only with
<?php echo $content_for_layout ?>
and then in you action where you're getting your blob data use that layout
$this->layout = 'empty.ctp';
and also to disable debugging, in your controllers use
Configure::write('debug',0);
if you're unable to create new layout you could try this.
$this->layout = null;
$this->render("view_name");
If you're using this to download files, you should use the Media view in cakePHP
http://book.cakephp.org/view/1094/Media-Views
$this->view = 'Media';
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip', // must be lower case
'path' => APP . 'files' . DS // don't forget terminal 'DS'
);
CakePhp 2.3 users :
use Sending files from the Book
CakePhp 2.x users :
use '$this->viewClass' instead of '$this->view'
copy-paste ready full solution, right in any controller file:
<?php
public function download($file) {
$fsTarget = APP.WEBROOT_DIR.DS.'files'.DS.$file; // files located in 'files' folder under webroot
if (false == file_exists($fsTarget)){
throw new NotFoundException(__('Invalid file'));
}
$pathinfo = pathinfo($fsTarget);
$this->viewClass = 'Media';
$params = array(
'id' => $file,
'name' => $pathinfo['filename'], // without extension
'download' => true,
'extension' => $pathinfo['extension'], // must be lower case
'path' => dirname($fsTarget) . DS // don't forget terminal 'DS'
);
$this->set($params);
}
Hope this helps!

Resources