Web Analytics Made Easy -
StatCounter Downloaded file is always corrupted? - CodingForum

Announcement

Collapse
No announcement yet.

Downloaded file is always corrupted?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Downloaded file is always corrupted?

    Hey people,

    I have an odd problem. I have the following page: http://www.mediamogulsweb.com/price_list.php

    There is a download link there to grab the PDF file version of the page....

    The downloaded file is always corrupted, and I can;t figure out why...You can view the file manually (inside the browser) just fine, if you type in the file name in the address bar.

    I've used this code before and never had problems with it. Here is the relevant code...Any help is appreciated:

    PHP Code:
    <?

    ob_start
    ();

    // Prompt user to download file if they clicked on an upload file link

    function my_mime_content_type($file)
    {
        return 
    system(trim('file -bi ' escapeshellarg($file)));
    }

    if (!empty(
    $_GET['download_file']))
    {
        
    $am_file urldecode($_GET['download_file']);
        
    $am_file_path "/var/www/html/" $am_file;

        
    // die($am_file . " " . $am_file_path);

        
    $mime_type my_mime_content_type($am_file_path);

        
    header('Content-type: '.$mime_type);
        
    header('Content-Disposition: attachment; filename="'.$am_file.'"');
        
    readfile($am_file_path);
    }

    ob_end_flush();

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">

    <head>

    <!-- MORE CODE HERE -->

    <div id="heading_container">
        <div id="heading1"><img src="MM_logo_grey.jpg" /></div>
        <div id="heading2" title="Download"><a href="<? echo $_SERVER['PHP_SELF']; ?>?download_file=<? echo 'web_prices.pdf'?>">Download this as a</a><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<? echo $_SERVER['PHP_SELF']; ?>?download_file=<? echo 'web_prices.pdf'?>"><b>PDF file</b>. <img src="pdf_icon.gif" border="0" /></a></div>
        <div style="clear:both"></div>
    </div>
    Last edited by chump2877; Oct 11, 2006, 10:17 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
    Explore all products and services, view demos, review documentation, check prices, and more!
    ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #2
    It just opened in my Opera
    You should send a Content-Length header.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

    Comment


    • #3
      I'm not so sure about using the Content-Length header, because the size of any given file seems to vary depending on where the file is stored and how it's being read...So instead of inviting more problems by adding that header, I just decided to put a die() command immediately following readfile() so that readfile() couldn;t possibly lump in any additional data (i.e., the rest of the code in that file) with the PDF download....and this seems to have worked....

      So the code is now:

      PHP Code:
      <?

      ob_start
      ();

      // Prompt user to download file if they clicked on an upload file link

      function my_mime_content_type($file)
      {
          return 
      system(trim('file -bi ' escapeshellarg($file)));
      }

      if (!empty(
      $_GET['download_file']))
      {
          
      $am_file urldecode($_GET['download_file']);
          
      $am_file_path "/var/www/html/" $am_file;

          
      // die($am_file . " " . $am_file_path);

          
      $mime_type my_mime_content_type($am_file_path);

          
      header('Content-type: '.$mime_type);
          
      header('Content-Disposition: attachment; filename="'.$am_file.'"');
          
      readfile($am_file_path);
      }

      ob_end_flush();

      if (!empty(
      $_GET['download_file']))
      {
          die();
      }

      ?>
      Regards, R.J.

      ---------------------------------------------------------

      Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
      [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
      Explore all products and services, view demos, review documentation, check prices, and more!
      ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

      Comment


      • #4
        The file size should always be the same...
        I'm not sure if this was any help, but I hope it didn't make you stupider.

        Experience is something you get just after you really need it.
        PHP Installation Guide Feedback welcome.

        Comment

        Working...
        X