...

View Full Version : how to add a download link using PHP



littlestone
07-29-2011, 02:32 AM
Hello, All

Suppose I have an excel file Test.xlsm. I want to add a download link in my page so that people can download this file. So I write the following codes:


<a href="Test.xlsm">Test.xlsm</a>

However, when I click on the link, I got some weird code.

I have searched online. Some people say I can use PHP to make such download link work. But I don't know how to write such code. Can anyone help me?

Thank you.

djh101
07-29-2011, 02:59 AM
http://webdesign.about.com/od/php/ht/force_download.htm

There you go.

littlestone
07-29-2011, 03:58 AM
Thank you.

I actually find a similar web. The following is my code:

test.php:

<?php
$file="test.xlsm";
header('Content-type: application/vnd.ms-excel.sheet.macroEnabled.12');
header('Content-disposition: attachment; filename="test.xlsm"');
readfile('$file');
?>


html code:

<a href="test.php">test.xlsm</a>

However, the download is still not successful: test.xlsm actual size is 42k, but the downloaded one is 0 byte.

What could be wrong?

tangoforce
07-29-2011, 04:22 AM
However, when I click on the link, I got some weird code.


It's always useful to share these weird codes. It might not mean much to you but it usually does to someone else.

Also if you look at your first post you use a file name of 'Test.xlsm' but in your second post you use 'test.xlsm'. If you are on a linux server it may be case sensitive and using a capital T or lowercase T could screw this up.

djh101
07-29-2011, 04:27 AM
$file shouldn't be in quotes. Although you can put "test.xlsm" into readfile(), there's really no need to use a variable.

^ And that.

littlestone
07-29-2011, 05:51 AM
Thank you all.
I finally used method djh101 recommended and the code worked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum