Linking to a file (e.g. PDF) within a Cake

2020-07-09 09:16发布

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.

标签: cakephp
8条回答
唯我独甜
2楼-- · 2020-07-09 09:57

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.

查看更多
手持菜刀,她持情操
3楼-- · 2020-07-09 10:02

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>
查看更多
欢心
4楼-- · 2020-07-09 10:02

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...

查看更多
聊天终结者
5楼-- · 2020-07-09 10:13
$html->link('Pdf', '/files/myfile.pdf');
查看更多
smile是对你的礼貌
6楼-- · 2020-07-09 10:14

This should work

<?php echo $html->link('pdf', $this->webroot('files'.DS.'test.pdf'); ?>
查看更多
女痞
7楼-- · 2020-07-09 10:15

or...

<a href="<?php echo $html->url('/files/somefile.pdf'); ?>">Link Text</a>
查看更多
登录 后发表回答