简体   繁体   English

在href中插入php变量

[英]Insert php variable in a href

I am planning to insert a PHP variable which holds the directory path for a file stored on my Windows machine. 我打算插入一个PHP变量,它保存存储在我的Windows机器上的文件的目录路径。 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脚本中的href标记中包含此变量,以便当用户单击此链接时,应将其重定向到该特定文件夹和文件。

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 谢谢

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. 但是你的路径是相对于服务器的,不要忘了。

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页面上插入路径,然后将其发送到服务器?

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>;

Try using printf function or the concatination operator 尝试使用printf函数或concatination运算符

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM