在 href 中插入 php 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8256463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert php variable in a href
提问by 125369
I am planning to insert a PHP variable which holds the directory path for a file stored on my Windows machine. How can I include this variable in the a href
tag inside my php script such that when the user clicks this link it should be redirected to that particular folder and file.
我打算插入一个 PHP 变量,该变量包含存储在我的 Windows 机器上的文件的目录路径。如何将这个变量包含href
在我的 php 脚本中的 a标签中,这样当用户点击这个链接时,它应该被重定向到那个特定的文件夹和文件。
For ex: $folder_path = 'C:\docs\test\file1.txt';
例如: $folder_path = 'C:\docs\test\file1.txt';
Right now I have tried some different ways but with no success. I have also did some research on internet, but alas could not find a proper answer.
现在我尝试了一些不同的方法,但没有成功。我也在互联网上做了一些研究,但遗憾的是找不到正确的答案。
If any one has an idea would be thankful if it can be shared. Thanks
如果有人有想法,如果可以分享,将不胜感激。谢谢
回答by LostMohican
You could try:
你可以试试:
<a href="<?php echo $directory ?>">The link to the file</a>
Or for PHP 5.4+ (<?=
is the PHP short echo tag):
或者对于 PHP 5.4+(<?=
是 PHP短回显标签):
<a href="<?= $directory ?>">The link to the file</a>
But your path is relative to the server, don't forget that.
但是你的路径是相对于服务器的,不要忘记这一点。
回答by Strae
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 public htdocs directory, it will not work.
请注意,您必须使用相对于您的域的路径,如果文件夹路径在公共 htdocs 目录之外,它将不起作用。
EDIT: maybe i misreaded the question; you have a file on your pc and want to insert the path on the html page, and then send it to the server?
编辑:也许我误读了这个问题;您的电脑上有一个文件,想在html页面上插入路径,然后将其发送到服务器?
回答by Basel Sayeh
in php
在 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>;
回答by Ed Heal
Try using printf
function or the concatination operator
尝试使用printf
函数或连接运算符