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.
$html->link('Pdf', '/files/myfile.pdf');
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
pdf
it should output
pdf
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..
Link Text
:)
or...
Link Text
Descargar Archivo
Related
I did some custom scripting in a custom module. After my scripting I would like to go to the file path of a file field.
How can I achive this?
I have the uri of the file to display it.
I tried
drupal_goto('$result')
where $result is result of query with uri field of file.
Although
drupal_goto($result) does not work.
Any suggestions??
drupal_goto(file_create_url($result));
The uri contains public://
If this doesnt work, print the result of file_create_url($result) and copy past it in your browser see if the file is really there. By the way, you should avoid querying the database just for one file and use a file_load() to avoid bad surprises.
Thanks this realy helped me in the good direction...
Now I used this
$file = file_load($fid);
$uri = $file->uri;
$url = file_create_url($uri);
drupal_goto($url);
Hope this will help someone :-)
I have a plugin installed that has its own layout overrides for different controllers. However I'm having trouble understanding the mechanism for modifying the paths.
In the plug-in controller if I tell it to use my layout
$this->layout = 'default_dashboard';
Which is in app/Views/Layout and references an image in app/webroot/default_images.
All the relative links work fine to default_images when I do this, but would like to use some of the Plugin template overides for other actions.
However if I modify the default.cpt file to include some of the images, like say a logo that is used in default_dashboard.ctp. It is unable to map to the same image location.
For example in default.ctp:
echo $this->Html->image('default_images/logo.png',array('alt' =>
'Logo','width'=>'284','height'=>'82'));
produces a path to /img/default_images/logo.png. The Plugin is configured to use the /img location, whereas I want to direct to /default_images in this case. I could make this ../default_images/logo.png, but this isn't very clean.
In addition I have js and css which is having a similar problem. Can someone please explain the mechanism for using a site-wide default.ctp so that it works with inherited plugin templates?
From hard coding the links into the template not using the Html Helper, I see that the browser's relative path is confused because of the routing. For example the first one works with the root specified, the second doesn't.
<img src="/default_images/logo.png" alt="works" width='284' height='82'>
<img src="default_images/logo.png" alt="lost" width='284' height='82'>
What's the best way to make sure that the Plugin layouts and non-plugin layouts can all find the correct path to /default_images ?
Following are the steps that you can follow to resolve relative path problem:
Create a file abc_constants.php in app\Config folder.
Include the file in app\Config\bootstrap.php
require_once(abc_constants.php);
abc_constants.php should contain:
define('HTTP_HOST', "http://" . $_SERVER['HTTP_HOST'].'/');
define('SITE_URL', HTTP_HOST.'your_app_name/');
define('IMAGE_HTTP_PATH', SITE_URL.'app/webroot/default_images/');
Use these constants in your view file accordingly.
<?php echo $this->Html->image(IMAGE_HTTP_PATH.'logo.png',array('alt' => 'Logo','width'=>'284','height'=>'82'));
It looks a bit lengthy process at first time, but once implemented, you can use these constants in Ajax calls in view files, controller's code etc.
I am using cake 1.3 and i had tried to implement favicon..But some strang error happens and sometime its showing favicon but for sometimes its not showing it.Pronlem is cakephp img folder path is changing>How can i escape from this problem.
i have used below code in my default.ctp
<?php echo $this->Html->meta('favicon.ico','../../app/webroot/img/favicon.ico',array('type' => 'icon'));?>
Favicon shows for following url.
http://localhost/finalportal/index.php/events/eventlist
Favicon not showing for followin url
http://localhost/finalportal/index.php/productsServices
I had tried this also.
<?php echo $this->Html->meta('favicon.ico',/favicon.ico',array('type' => 'icon'));?>
in this case favicon path is not correct
What i am doing wrong
meta('favicon.ico',$this->webroot.'img/favicon.ico',array('type' => 'icon'));
It works perfectly, just create favicon.ico image in img folder.thats it.
Don't use relative paths in the HtmlHelper, Cake prepends the correct path for you.
I haven't use the meta function, so I'm not sure if it respects the Cake directory conventions (e.g. images are in img, JavaScripts in js), but this should work:
<?php echo $this->Html->meta('favicon.ico','/img/favicon.ico',array('type' => 'icon'));?>
use following code to get right path.
<?php echo $this->Html->meta('favicon.ico',$this->webroot.'img/favicon.ico',array('type' => 'icon')); ?>
The problem in the first version is that you're using relative paths so it will always end up pointing to the wrong place, depending on how many parameters the URL has. ../../app/webroot means "two levels down, then to directory app/webroot". Two levels down from http://localhost/finalportal/index.php/events/eventlist is http://localhost/finalportal/index.php/, but two levels down from http://localhost/finalportal/index.php/productsServices is http://localhost/finalportal/, so you end up in the wrong place.
The reason why the second method (which is syntactically correct) doesn't work is probably because you have the server set up wrong. Apache's DocumentRoot should point at the app/webroot directory or the .htaccess file in the root directory should redirect requests to app/webroot.
I am using cake php now. I have two file example1.ctp and example2.ctp. I want to include example2.ctp in example1.ctp just like how we add php page using "include". I am new to this please suggest me how to do it.
According to me , You have to create Elements..
CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
<?php echo $this->element('ur elements ctp file name'); ?>
Can you help me how to use class defined in CakePHP's /config/database.php file, for my custom scripts? I need to use defined array for db connection.
Tnx in adv!
There is nothing magic about this class. It works the same as any other PHP class.
<?php
include("path/to/cake/config/database.php");
$db = new DATABASE_CONFIG;
echo $db->default['login'];
echo $db->default['password'];
echo $db->default['database'];
?>
Now you can reference the variables like you would with any other class.
It sounds like you're looking for something similar to Rails' migrations, i.e. scripts that run within the context of the framework. There have been efforts to create such a thing (see Joel Moss' article in the Bakery).
What I tend to do is to keep a set of DDL scripts in a _meta/ directory of my project. I execute these directly on the database so they have no need to read info from database.php. If you're set on using Cake's runtime context, my way won't help you, but maybe Joel's will.