Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    how to add a download link using PHP

    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.

  • #2
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    626
    Thanks
    51
    Thanked 67 Times in 67 Posts
    "Yeah science!"
    Online Science Tools

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Quote Originally Posted by littlestone View Post
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    626
    Thanks
    51
    Thanked 67 Times in 67 Posts
    $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.
    "Yeah science!"
    Online Science Tools

  • Users who have thanked djh101 for this post:

    littlestone (07-29-2011)

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you all.
    I finally used method djh101 recommended and the code worked.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •