Results 1 to 16 of 16

Thread: [RESOLVED] How to allow downloading of a file

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Resolved [RESOLVED] How to allow downloading of a file

    I tried googling but perhaps I don't have the right keywords so I am asking here. As the title suggests, how can I allow a link in my site to download a file when it is clicked?

    TIA
    Last edited by dee-u; Aug 31st, 2009 at 10:09 PM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to allow downloading of a file

    this?
    PHP Code:
    <?php
    // We'll be outputting a PDF
    header('Content-type: application/pdf');

    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="downloaded.pdf"');

    // The PDF source is in original.pdf
    readfile('original.pdf');
    ?>
    from the examples of header() on PHP.net.

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    Thanks, I will try that when I have more time to fiddle with it. I actualy have a application which I want to share to the public so I want it to be downloadable, it is an Exe so will there be any special modification that I have to make to that sample you gave?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to allow downloading of a file

    change the files names, that's about it.

    but if you're just giving people one file, then you could just link to the exe file itself?

  5. #5

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    How do I provide a link to the exe itself? Sorry but if its plain html then I have totally forgotten it being a desktop developer for years.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to allow downloading of a file

    ... are you kidding?

    Code:
    <a href="http://website.com/file.exe">download</a>

  7. #7

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    No I am not. =) I haven't really previously tried such so I didn't know. What would be the difference of that and the one that uses PHP as in your post #2?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: How to allow downloading of a file

    If you link to a file format that a browser thinks it can open by itself (like .pdf, .txt, .html, .php, .jpg, etc.), then it will usually try to do so. If you want the browser not to try to open it, and instead prompt the user to save the file, then you use something like kow's post #2. But most browsers will not try to open an .exe, and will automatically go to the download prompt simply by linking to them.

  9. #9

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    Quote Originally Posted by kows View Post
    ... are you kidding?

    Code:
    <a href="http://website.com/file.exe">download</a>
    Somehow this does not work to me. I only used

    Code:
    a href="file.exe
    Using Opera upon clicking the link it does not continue, it just keep loading. Using IE it went to site/file.exe but does not prompt to download the file, why is this?

    The file is 3MB if it makes any special treatment. I have even tried the script found here but I cannot make it work. Its giving me an error
    Warning: opendir(/downloads/) [function.opendir]: failed to open dir: No such file or directory in /home/a9056016/public_html/download.php on line 95
    and
    Warning: readdir(): supplied argument is not a valid Directory resource in /home/a9056016/public_html/download.php on line 97
    I will keep looking into this but I hope someone else more knowledgeable on this could pinpoint to me what I am doing wrong.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: How to allow downloading of a file

    Quote Originally Posted by dee-u View Post
    Somehow this does not work to me. I only used
    Code:
    a href="file.exe
    What do you mean by that?... You need to use the whole snippet that kows gave: <a href="http://website.com/file.exe">download</a> ...

    The errors you're encountering with the download script are probably because you haven't set it up properly - on line 22, see this line:
    Code:
    define('BASE_DIR','/home/user/downloads/');
    This specifies the path to the file(s) you want to download. The errors would suggest that you've left this as the default value, and not customized it with your own path.

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to allow downloading of a file

    since you're on a linux server, if you want to make a relative file path use this format:

    define('BASE_DIR','./downloads/');

    otherwise, using a slash in the beginning will start looking at the root of your filesystem. if you do this, you'll need to include the full path to your web directory, which seems to be '/home/a9056016/public_html/', so your BASE_DIR might look like this if you don't use the method above:

    define('BASE_DIR','/home/a9056016/public_html/downloads/');

  12. #12

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    Quote Originally Posted by kows View Post
    since you're on a linux server, if you want to make a relative file path use this format:

    define('BASE_DIR','./downloads/');

    otherwise, using a slash in the beginning will start looking at the root of your filesystem. if you do this, you'll need to include the full path to your web directory, which seems to be '/home/a9056016/public_html/', so your BASE_DIR might look like this if you don't use the method above:

    define('BASE_DIR','/home/a9056016/public_html/downloads/');
    Cool, that did it, I did try '/downloads/' without the period. Thanks!

    EDIT: Darn, got to spread first before I can rate you again. =(
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    Sorry, I am now able to offer a download but it seems that there is a problem when the file is being downloaded, it just stops in the middle of downloading, here is the script of the download code, could anyone point out what's wrong?

    Code:
    $file = @fopen($file_path,"rb");
    if ($file) {
      while(!feof($file)) {
       set_time_limit(0);
        print(fread($file, 1024*8));
        flush();
        if (connection_status()!=0) {
          @fclose($file);
          die();
        }
      }
      @fclose($file);
    }
    I've even attempted to compress the file and it is being downloaded completely but when one tries to open the compressed file it says it is corrupted and cannot be extracted.

    I hope someone here could shed light on this. Thanks!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How to allow downloading of a file

    dee-u,

    I did not quite follow why you have tried to use PHP to stream the file out rather than providing a direct link to it.

    As long as your web server is configured to serve files with an '.exe' extension as application/octet-stream and with Content-disposition:attachment (which nearly all web servers are by default) then you will have no problems and you avoid having to invoke the PHP interpreter for a task which really does not concern it.

    The only reason I can think of for streaming a file via PHP is to provide access control based on custom authentication logic. Even in this case I would advise that you try instead generating a temporary link and redirecting the client to that, rather than streaming through PHP.

  15. #15

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to allow downloading of a file

    Hmmmnnn... Thanks for pointing it out, I think I had a problem with a direct link for the exe, what I tried now is to zip the file and just used a direct link to the zipped exe, I'm still testing it if it will work.

    Thanks!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  16. #16

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] How to allow downloading of a file

    The direct link to the zip file works without hassle.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

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



Click Here to Expand Forum to Full Width