Linking to a file (e.g. PDF) within a CakePHP view
-
04-07-2019 - |
Question
I'd like to link to some PDFs in one of my controller views. What's the best practice for accomplishing this? The CakePHP webroot folder contains a ./files/ subfolder, I am confounded by trying to link to it without using "magic" pathnames in my href (e.g. "/path/to/my/webroot/files/myfile.pdf").
What are my options?
EDIT: I didn't adequately describe my question. I was attempting to link to files in /app/webroot/files/ in a platform-agnostic (ie. no mod_rewrite
) way.
I've since worked around this issue by storing such files outside the CakePHP directory structure.
Solution
$html->link('Pdf', '/files/myfile.pdf');
OTHER TIPS
This is somewhat tangential, but for access to such a location in Models and other places you can simply do this:
$file = WWW_ROOT . DS . 'files' . DS;
This tactic might be helpful to someone accessing files for static data loading, such as XML or JSON.
This is not recommended for public consumption or public linking.
I can confirm that this is an issue when mod_rewrite is not being used.
<?php echo $html->link('pdf', '/files/test.pdf'); ?>
outputs
<a href="/pathtoapp/index.php/files/test.pdf">pdf</a>
it should output
<a href="/pathtoapp/app/webroot/files/test.pdf">pdf</a>
This should work
<?php echo $html->link('pdf', $this->webroot('files'.DS.'test.pdf'); ?>
I'm not sure I understand the question correctly, but here goes. Basically any file you put in the webroot folder will be accessible on the webserver, so if you put the file in webroot/files/file.pdf you would simply link to /files/file.pdf.
If that doesn't work, please clarify your question...
or..
<a href="<?php echo $this->webroot; ?>files/somefile.pdf">Link Text</a>
:)
or...
<a href="<?php echo $html->url('/files/somefile.pdf'); ?>">Link Text</a>
<a href="<?php echo $this->request->webroot . 'carpetadentrodelwebroot/archivo.pdf'; ?>" target="pdf-frame" download="nombreParaDescarga">Descargar Archivo</a>