www.webdeveloper.com
Results 1 to 4 of 4

Thread: Issue When Attempting to Force 'Save File' Prompt with PHP

  1. #1
    Join Date
    Jan 2012
    Posts
    2

    Issue When Attempting to Force 'Save File' Prompt with PHP

    Hello,

    I'm attempting to create a form button which allows users to simple click to download mp3 files from my website. So far I have successfully created a button which, when clicked, forces the browsers 'Save File' prompt. However, once the download begins, it fails after downloading around 0.5kb.

    The code I'm using is as follows...

    /DownloadPHP/All_Eyes_On_You_Acoustic.php
    Code:
    <?php
    header('Content-disposition: attachment; filename=/Tracks/All_Eyes_On_You_Acoustic.mp3');
    header('Content-type: audio/mpeg');
    readfile('/Tracks/All_Eyes_On_You_Acoustic.mp3');
    ?>
    /index.php
    Code:
    <form><input type="button" value="Download" onClick="window.location.href='DownloadPHP/All_Eyes_On_You_Acoustic.php'"></form>


    My understanding of PHP is essentially null, and the above code is in fact the result of a number of google searches on the subject, so I am at a complete loss as to how to fix this issue, and I've been unable to find a solution thus far.

    The reason for using individual files to change the header content disposition, rather than applying it to all files of that type, is because I also wish to include a link on the page which will open the file within the browser.


    Appriciate any replies.

  2. #2
    Join Date
    Aug 2008
    Posts
    13
    Hmm. You shouldn't provide a path in header command. Really all the filename flag in the header is just telling the browser what to name the file when downloaded.

    Try changing from

    Code:
    header('Content-disposition: attachment; filename=/Tracks/All_Eyes_On_You_Acoustic.mp3');');
    to

    Code:
    header('Content-disposition: attachment; filename="All_Eyes_On_You_Acoustic.mp3"');
    and see if that works

  3. #3
    Join Date
    Jan 2012
    Posts
    2
    Thanks ScottAN

    I've change this but it hasn't resolved the issue unfortunately.

  4. #4
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Try this too...

    PHP Code:
    readfile($_SERVER['DOCUMENT_ROOT'].'/Tracks/All_Eyes_On_You_Acoustic.mp3'); 
    Make sure the path is correct, use echo and file_exist() to debug.
    Last edited by MrCoder; 01-09-2012 at 06:20 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles