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

  • SEARCH
  • Home
  • 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 8176329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:07:45+00:00 2026-06-06T23:07:45+00:00

Another problem with PHP force download. It was working and then i did something

  • 0

Another problem with PHP force download. It was working and then i did something and now its not.

Here we go (there are if statements for each file type I accept but just showing one should be enough to help diagnose the problem):

$string1 = 'home/myhost/aboveroot/reviews/';
$string2 = $username;
$string3 = '/';
$string4 = $filename;
$file= $string1.$string2.$string3.$string4;
$ext = strtolower (end(explode('.', $filename)));
//Finding MIME type
    if ($ext = pdf){
        header("Content-disposition: attachment; filename= '$filename'");
        header('Content-type: application/pdf');
        readfile('$file');

What ends up happening is that it forces a file download with the correct file name and of the correct extension. It doesn’t trigger a corrupt file error of any sort. However, when I open the file the content is missing. If its a word document it will refuse to open. If its a text document the text will be changed to a bunch of php errors as follows:

Warning: readfile($file) [function.readfile]: failed to open stream: No such file or directory in /home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php on line 68

Then it gives this error for each line with a header (even those contained in if clauses that should not be meeting their conditions:

Warning: Cannot modify header information – headers already sent by (output started at /home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php:68) in /home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php on line 69

I’m really lost. Any help would be greatly appreciated. I finally get everything on my site working and BAM, I realize that im having this huge mess of a 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-06-06T23:07:49+00:00Added an answer on June 6, 2026 at 11:07 pm

    This line is wrong :

    if ($ext = pdf){
    

    It should be :

    if ($ext == "pdf"){
    

    And you could add :

    if($ext == "pdf" && file_exists($file)) {
    

    to be sure that you get a valid file.

    And this is wrong too :

    readfile('$file');
    

    It should be :

    readfile($file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now here is another problem.. my code: <script type=text/javascript> function validate_form () { if
sorry, I have another problem now. I'm using a new php form method than
I've got another problem (which might not be an issue in terms of coding
Sometimes I do despair when working with ASP.Net - another problem that shouldn't be!
After getting a helpful answer here , I have run into yet another problem:
I'm having another problem with this exercise: http://lpmj.net/20.php My problem is that when i
Right now, I am using a custom PHP solution to force WWW to be
(LAMP server configuration) As a workaround for another problem, I need PHP to be
I have another problem with my scripy now I have made it more advance,
I am facing a another problem. I built a project in php. the url

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.