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 6085185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:36:38+00:00 2026-05-23T11:36:38+00:00

I have a pdf file which is located off my webpage’s root. I want

  • 0

I have a pdf file which is located off my webpage’s root. I want to serve a file in ../cvs to my users using php.

Here is the code I have sofar:

header('Content-type: application/pdf');

$file = file_get_contents('/home/eamorr/sites/eios.com/www/cvs/'.$cv);
echo $file;

But when I call this php page, nothing gets printed! I’d like to simply serve the pdf file stored whose name is in $cv (e.g. $cv = 'xyz.pdf').

The ajax response to this PHP page returns the text of the pdf (gobbldy-gook!), but I want the file, not the gobbldy-gook!

I hope this makes sense.

Many thanks in advance,


Here’s the AJAX I’m using

$('#getCurrentCV').click(function(){
    var params={
        type: "POST",
        url: "./ajax/getCV.php",
        data: "",
        success: function(msg){
            //msg is gobbldy-gook!
        },
        error: function(){

        }
    };
    var result=$.ajax(params).responseText;
});

I’d like the user to be prompted to download the file.

  • 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-23T11:36:39+00:00Added an answer on May 23, 2026 at 11:36 am

    Don’t use XHR (Ajax), just link to a script like the one below. The HTTP headers the script outputs will instruct the browser to download the file, so the user will not navigate away from the current page.

    <?php
    // "sendfile.php"
    
    //remove after testing - in particular, I'm concerned that our file is too large, and there's a memory_limit error happening that you're not seeing messages about.
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    
    $file = '/home/eamorr/sites/eios.com/www/cvs/'.$cv;
    
    //check sanity and give meaning error messages
    // (also, handle errors more gracefully here, you don't want to emit details about your
    //  filesystem in production code)
    if (! file_exists($file)) die("$file does not exist!");
    if (! is_readable($file)) die("$file is unreadable!");
    
    //dump the file
    header('Cache-Control: public'); 
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="some-file.pdf"');
    header('Content-Length: '.filesize($file));
    
    readfile($file);
    
    ?>
    

    Then, simplify your javascript:

    $('#getCurrentCV').click(function(){
         document.location.href="sendfile.php";
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PDF file which I want to verify whether the links in
I have a pdf reader app which render the pdf file. It works fine
I have some images and pdf file on my server which I use in
I have an ASP.net application which returns a binary PDF file (stored from the
I have a pdf file on my server. I want to create such link
I have a PDF file which contains just 1 Page. I have a barcode
I have one pdf file which is extract from ppt (power point presentation) and
I have an existing pdf file which needs to be updated with information which
I have an encrypted word/excel/pdf file locally stored which I need to preview in
I have to read a pdf file which contains a table with several columns.

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.