15

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.

For ex: $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

1
  • By the way once your page is place on the web server the link to your local machine will not work. You will have to upload the file to the server. Commented Nov 24, 2011 at 11:45

4 Answers 4

32

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):

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

But your path is relative to the server, don't forget that.

Sign up to request clarification or add additional context in comments.

Comments

21
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.

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?

Comments

4

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

Comments

0

Try using printf function or the concatination operator

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.