Url of word document

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stribor
Forum Newbie
Posts: 12
Joined: Fri Apr 01, 2011 10:48 pm

Url of word document

Post 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"?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Url of word document

Post by twinedev »

did you upload it? what directory did you browse to when you uploaded it?
stribor
Forum Newbie
Posts: 12
Joined: Fri Apr 01, 2011 10:48 pm

Re: Url of word document

Post by stribor »

Yes i did.
It is here /var/www/username/directory1/directory2/mydoc.doc
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Url of word document

Post 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
stribor
Forum Newbie
Posts: 12
Joined: Fri Apr 01, 2011 10:48 pm

Re: Url of word document

Post 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
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Url of word document

Post 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
stribor
Forum Newbie
Posts: 12
Joined: Fri Apr 01, 2011 10:48 pm

Re: Url of word document

Post 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
stribor
Forum Newbie
Posts: 12
Joined: Fri Apr 01, 2011 10:48 pm

Re: Url of word document

Post by stribor »

Hi...i solved it. I had to add "http://" to my hyperlink...
thanks for help :)
Post Reply