Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:12:51+00:00 2026-05-25T01:12:51+00:00

I have a simple question. I was looking for function to download file. I

  • 0

I have a simple question. I was looking for function to download file. I have found one but I have small problem.

First, this is some codes of function:

header ('Content-type: ' . mime_content_type($file));
header ('Content-Disposition: attachment; filename="'.basename($file).'"');
header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT');
header ('Accept-Ranges: bytes');
header ('Cache-control: no-cache, must-revalidate');
header ('Pragma: private');

To test function I am using this:

$file = 'path/file.rar';
dowload_file($file);

It’s working fine and file is getting downloaded. But if I use the function for image, for example:

$file = 'path/image.jpg';
download_file($file);

Then the image is also getting downloaded. But I want it to be viewed and not downloaded.

If I delete this line:

header ('Content-Disposition: attachment; filename="'.basename($file).'"');

the image is showed immediately but the rar and zip files is stopped being downloaded.

So how to make images being displayed without downloading but any other file being downloaded? I am testing on localhost, may be this is the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:12:52+00:00Added an answer on May 25, 2026 at 1:12 am

    If you have the Content-Disposition header set to attachment, it will force a download regardless of file type.

    If you want your images to be handled differently, you’ll have to include a conditional on the Content-Disposition header, like:

    if (!preg_match('@\.(gif|jpe?g|png)$@', basename($file)) {
        // this is *not* an image, do C-D: attachment header
        header ('Content-Disposition: attachment; filename="'.basename($file).'"');
    } // otherwise do nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question related to one-line programming. First an example: function test(a)
I have one simple question about throwing exeptions in re.match function Is there a
This is probably a real simple question but I'm looking for the most memory
All, This should be a pretty simple question, but I haven't found quite the
I have a simple question, but I'm about 80% sure that the answer to
I might be looking at this the wrong way, but I have a form
I have been looking all over the Internet for an answer to this question
This question is simple, but the set-up's a bit complex. Please bear with me...
This should be simple but nothing's working: Question How do you set the height
Sorry if this question may be simple, but I can't seem to find an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.