Force Download Files in PHP

One of the most common problems that we find is to force the download of a certain file from the client’s browser, thus preventing it from opening.
For this we can use the following function in PHP.

function force_download($file)
    if ((isset($file))&&(file_exists($file))) {
       header("Content-length: ".filesize($file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
    } else {
       echo "No selected file";


107 responses to “Force Download Files in PHP

  1. just go to

    Hey! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!

  2. ?????

    Hey There. I found your blog using msn. This is a very well written article.I will make sure to bookmark it and come back to read more of your useful information. Thanks for the post.I’ll definitely comeback.

  3. onlinecasino

    Everyone loves what you guys tend to be up too.This type of clever work and exposure! Keep up the terrific works guys I’ve incorporated you guys to my own blogroll.

  4. nutricionista en Palma

    Having read this I believed it was very informative. I appreciate you spending some time and effort toput this article together. I once again find myself spending a lot of time both reading andcommenting. But so what, it was still worth it!

  5. inflatable water slides

    Very nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyed surfing around your blog posts. In any case I?ll be subscribing to your feed and I hope you write again soon!


    hi!,I like your writing so much! proportion we keep in touch more about yourpost on AOL? I need an expert on this house to unravel my problem.Maybe that is you! Taking a look ahead tolook you.

  7. here

    I like the valuable info you provide in your articles. I will bookmark your blogand check again here frequently. I am quite sure I’lllearn many new stuff right here! Best of luck for the next!

  8. Online

    Right now it seems like Movable Type is the top blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  9. you can check

    Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how could we communicate? gondolen tyringe meny

  10. click for more

    Hello There. I found your blog using msn. This is a really well written article.I’ll be sure to bookmark it and return to read more of youruseful information. Thanks for the post. I’ll certainly comeback.

  11. Look Here

    I needed to thank you for this good read!! I certainly enjoyed every little bit of it. I have got you saved as a favorite to look at new things you postÖ

  12. visit the website

    I’m curious to find out what blog platform you happen to be utilizing?I’m having some minor security issues with my latest blog and I would like tofind something more safeguarded. Do you have any solutions?asmr 0mniartist

Deja una respuesta

Tu dirección de correo electrónico no será publicada.

Rúa Pilar Miró n 6 Santiago de Compostela

Jersey St , Manchester, Greater Manchester M4 6JG, GB

T. 609 538 171 - 634 59 34 43