1

I have a file to be downloaded in a list. here is the link:

 <a href=" <?php echo $filepath; ?>"> download </a>

however when I hover on the download link, it displays this:

192.168.3.43/index.php/component/subscription/hello.txt

the $filepath variable only contains 'hello.txt'

Why is that happening? because of that, the web application misinterprets the file as a view of the component. I am getting frustrated..

Also, the link download only displays if it verifies the file exists. The hello.txt file is located on the directory of the apache server. If it does not find the file, the download link will not show. Since the download link was displayed, it was able to find the file.

Here is the image for reference.

enter image description here

2

The way you're creating your link gives you no control as to where the browser should look for the file. Your generated link is a relative link, meaning that the browser will look for the file in the current location. So if your current URL is http://example.com/index.php/component/subscription/, the file will be looked for in http://example.com/index.php/component/subscription/hello.txt.

There are several ways to get around this, depending on your needs. Let's say the file is located on your server in the JOOMLAROOT/mydownloads/ directory:

Option 1

<a href="http://example.com/mydownloads/<?php echo $filepath; ?>"> download </a>

This will work, but only if the domain is always the same. If you move your site or change domain, the link will break.

Option 2

<a href="/mydownloads/<?php echo $filepath; ?>"> download </a>

The first / will tell your browser to start looking from the top domain, whatever that is. The advantage is that it will work even if you change domains. The disadvantage is that if your Joomla installation is located in a subfolder of your domain (http://example.com/joomla), the link will still point to http://example.com/mydownloads/hello.txt

Option 3 (preferred method)

<a href="<?php echo JURI::base() ?>mydownloads/<?php echo $filepath; ?>"> download </a>

JURI::base() returns the complete path of your Joomla installation, whatever domain or subfolder it's located in. Using this method, you can generate an absolute link that always works. Note that the URI returned has a trailing "/".

Good luck.

0

That is a relative link, so the browser interprets it relative to the address of the page, presumably 192.168.3.43/index.php/component/subscription/. Convert it to an absolute link.

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.