Page 1 of 1

Url of word document

Posted: Tue Jan 10, 2012 10:22 pm
by stribor
I have word document "mydoc.doc" sitting in the directory inside my public_html.
My php script is running in the different directiry.
I would like my scipt to show link that when user click on it will open "mydoc.doc". How do i get URL of "mydoc.doc"?

Re: Url of word document

Posted: Wed Jan 11, 2012 12:11 am
by twinedev
did you upload it? what directory did you browse to when you uploaded it?

Re: Url of word document

Posted: Wed Jan 11, 2012 6:45 am
by stribor
Yes i did.
It is here /var/www/username/directory1/directory2/mydoc.doc

Re: Url of word document

Posted: Wed Jan 11, 2012 7:52 am
by twinedev
then assuming the root of the website is at /var/www/username/ the link should be:

Code: Select all

<a href="/directory1/directory2/mydoc.doc">Open Nasty Mircosoft Word Document (LOL)</a>
One way you can make sure is to run this script on your site:

Code: Select all

<p>The root of this site is at: <?php echo $_SERVER['DOCUMENT_ROOT']; ?></p>
(see http://www.php.net/reserved.variables.server.php for more goodies you can get from $_SERVER)

It is good practice to put absolute links (starts off root of site, '/directory/whatever/file.html') instead of relative ones ( '/../whatever/file.html' if being called from /directory/otherdir/index.html). This way no matter where you move the file that links to the file around, it will always work.

-Greg

Re: Url of word document

Posted: Wed Jan 11, 2012 8:50 am
by stribor
Problem is when i have I make hyper link like this - > http://servername.com/var/www/username/ ... /mydoc.doc doesnt work
If i make it like this - > http://servername.com/username/director ... /mydoc.doc does work

Using php i can get path to the file but it always return this ->/var/www/username/directory1/directory2/mydoc.doc

Re: Url of word document

Posted: Wed Jan 11, 2012 9:02 am
by twinedev

Code: Select all

$strFile =  '/var/www/username/directory1/directory2/mydoc.com' ; // However you are getting it...
$strFile = substr($strFile,strlen($_SERVER['DOCUMENT_ROOT']));
-Greg

Re: Url of word document

Posted: Wed Jan 11, 2012 9:36 am
by stribor
that definetely worked...thank you so much.but have another problem.
for simplicuty lets assume that this variable is string holding path to my file $file.

now i want to do present clickable link like this.....

Code: Select all

 echo "<a href = '.$file."'>Click Here</a>";
And when I run it i am getting this link to be ...

current/working/directory/attached/to/$file....

how do i solve this

Re: Url of word document

Posted: Wed Jan 11, 2012 9:40 am
by stribor
Hi...i solved it. I had to add "http://" to my hyperlink...
thanks for help :)