Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to a file (e.g. PDF) within a CakePHP view

Tags:

cakephp

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.

like image 525
Daniel Wright Avatar asked Oct 24 '08 14:10

Daniel Wright


3 Answers

$html->link('Pdf', '/files/myfile.pdf');
like image 61
Alexander Morland Avatar answered Sep 27 '22 20:09

Alexander Morland


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.

like image 45
zmonteca Avatar answered Sep 27 '22 20:09

zmonteca


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>
like image 28
James Revillini Avatar answered Sep 27 '22 21:09

James Revillini