Insert php variable in href

I plan to insert a PHP variable that contains the directory path for the file stored on my Windows machine. How to include this variable in the href tag inside my PHP script so that when a user clicks this link, it should be redirected to this specific folder and file.

For ex: $folder_path = 'C:\docs\test\file1.txt';

Right now I tried several different ways, but without success. I also did some research on the Internet, but, alas, I could not find the right answer.

If anyone has an idea, it would be grateful if it could be shared. Thanks

+6
source share
4 answers
 echo '<a href="' . $folder_path . '">Link text</a>'; 

Please note that you must use the path relative to your domain, and if the folder path is outside the htdocs public directory, it will not work.

EDIT: maybe I misunderstood the question; Do you have a file on your computer and want to insert the path to the html page and then send it to the server?

+14
source

You may try:

 <a href="<?php echo $directory ?>">The link to the file</a> 

Or for PHP 5.4+ ( <?= Is PHP short echo tag ):

 <a href="<?= $directory ?>">The link to the file</a> 

But your path relative to the server, do not forget about it.

+15
source

in php

 echo '<a href="' . $folder_path . '">Link text</a>'; 

or

 <a href="<?=$folder_path?>">Link text</a>; 

or

 <a href="<?php echo $folder_path ?>">Link text</a>; 
+1
source

Try using the printf function or the concatenation operator

http://php.net/manual/en/function.printf.php

0
source

Source: https://habr.com/ru/post/902298/


All Articles