5

I have created menu link for a pdf file. I can use a full path like "http://website/sites/all/myfiles/file.pdf", but if I try to put a relative link such as "sites/all/myfiles/file.pdf" it does not work. If anyone has idea please tell me.

6

The thing with the menu system is that what you are typing is not a relative path, but an internal Drupal URL.

Since what you are after is not a Drupal path, but instead a static file server outside of Drupal by your webserver your options are limited.

  • You can write the full url like you tried yourself
  • You can create a module/theme where you can make this possible, either by adding the extra link or creating a drupal path that redirects to the static file.

Of these, I personally think that a an absolute path is the lessor of evils, unless you need to do this a lot of times.

|improve this answer|||||
5

Just use the Menu Token module. Copy your absolute PDF url to your path textfield and replace the servername with [site:url]. Also make sure the uses tokens checkbox is activated and save your menu item... In combination with the Menu Attributes module you could even link your PDF's to a blank browser tab/window if you like so.

|improve this answer|||||
  • This works great, except on my Drupal 6 installation I had to use [site-url] rather than [site:url]. I have Menu Token 6.x-1.0-alpha2+0-dev and Token 6.x-1.19 installed. – GuitarPicker Nov 5 '15 at 14:55
4

Drupal uses "clean URLs" so when you're on the node/123 page and you are visualizing a link such as:

<a href="sites/all/myfiles/file.pdf">PDF file</a>

Your browser will interpret it as "node/123/sites/all/myfiles/file.pdf" which won't work. You have to put a "/" at the beginning of the URL:

<a href="/sites/all/myfiles/file.pdf">PDF file</a>

Of course, if your site is in a subdirectory, you need to append it; the safest way is to use l():

<?php echo l('PDF file', 'sites/all/myfiles/file.pdf'); ?>

Edit Dic, 18: use l() as per kiamlaluno advice.

|improve this answer|||||
  • The correct way is to output a Drupal URL is to use url(); the function always output the correct URL, independently from Drupal using clean URLs (or not), or Drupal being installed in a sub-directory of the server root directory (or not). Actually, in such cases you should use l(), which outputs the <a> tag for a Drupal URL. – kiamlaluno Dec 18 '11 at 14:26
  • Thanks, I wasn't sure you could use l() without a Drupal internal path ;) – tostinni Dec 18 '11 at 18:37
0
  • Create a new content type (e.g. "file download")

  • Give it a field URL (a link, or a regular text field)

  • Override the template file used in your theme (e.g. node--file-download.tpl.php)
  • In this file, put the PHP code to load the field with the URL of file, and then put the following code:

    Header("Location: file_address"); 
    
  • Create a pattern in the Pathauto module for this kind of content type (e.g. "file/download/name" or "download/file_id")

  • Then, you can put it in the menu without problems

I bet there's an easier way, but this should also do the work. It should be double considered in a matter of security.

|improve this answer|||||
  • Thanks for advice. I also used like Create new content And add a code any of following <?php header('Content-Type: application/pdf'); header("Location: sites/all/myfiles/file.pdf"); ?> OR <script type="text/javascript"> window.open('sites/all/myfiles/File.pdf','MyFile','width=400,height=350'); </script> I just used menu link to the content and this content redirects to my pdf file. but I have about 100s of pdf. For which I need to create 100s of contents. So I am searchinhg for any other solution which is easy and safety for my site. – Abhijeet Dec 13 '11 at 11:49
  • So maybe use module Pages? Try creating: path : localhost:8888/drupal/download_file/%file_name variant_type : http response. and limit it to folder with those 100 files.. This seems to be easier. – wtk Dec 13 '11 at 13:03
0

Just use the Pathologic module, and your relative links will work just fine.

|improve this answer|||||
  • Pathlogic only works on node content as a filter. It would not work for menu items – picxelplay Jan 28 '12 at 14:19
0

You can also use Menu HTML module together with Special Menu Items module . This will allow you to place anchor pointing to you file as Menu link title, and nolink tag as menu path.

Just take a look at the screenshot:

File path as menu item

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.