SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Download and redirect in PHP

    Hi All,

    Almost all sites do this.i.e. When you click on the download button the file downloads and you are automatically redirected to a page saying 'If the download does not start automatically then click here'. How do I do this in PHP?

    This is what I have done.

    index.php
    <form method="post" action="download.php">
    <label for="txtFileName">Enter File Name/label>
    <input type="text" id="txtFileName" name="txtFileName" />
    <input type="submit" id="btnDownload" name="btnDownload" class="btn" value="Download"/>
    </form>

    download.php
    //After getting the matching file name from the database....
    header('Cache-Control: public');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename=".$filename.".pdf"');
    readfile($filename);

    This is how I want to work
    Once I enter the filename it should fetch the location of the file and download and it should redirect to the page which says "IF the file does not download automatically, then click here"

    but with the above code the file gets downloaded and I am still in index.php...
    please help me

    thanks

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried adding the HTML into the download.php page? What about something like below (untested)

    PHP Code:
    <?php header('Cache-Control: public');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename=".$filename.".pdf"');
    readfile($filename); ?>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    If your download does not begin automatically, click <a href="<?php echo $filename?>.pdf">here</a>.
    </BODY>
    </HTML>

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks limotek for the reply.
    I have tested this code. I am able to download the file but redirection I am not able to achieve redirection.

  4. #4
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For redirection:

    header("Location: url");

    is used.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Think about what's happening. A page loads with a link to download a file. After some time, the page automatically "goes to" that link. This is not done with php as php has finished by then. It could be done with javascript or a meta redirect (both of those are not 100&#37; reliable but that's why you have the text link).
    - Robert

  6. #6
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks,
    but is this how all the big sites do?

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep.

    Just look at their HTML source code. It's just a link to the file.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks arkinstall and QMonkey


Bookmarks

Posting Permissions

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