Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    washington, dc
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Arrow header() not allowing me to download the format

    I get an error everytime I run this script. It isn't pushing the file for download / opening it no matter what I do. I keep getting errors... But when I take off the header() it spits the byte to me. Ideas?

    PHP Code:
    <?php

         $ch 
    curl_init();
         
    curl_setopt($chCURLOPT_URL'http://www.mainwebsitelink.com/Download.do?action=download&dataId=55555&format=title_of_the_file_i_want');
         
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
         
    $contents curl_exec ($ch);
         
    curl_close ($ch);
         echo 
    $contents;
         
    $file $contents;

            
    header('Content-Description: File Transfer');
            
    header('Content-Type: application/pdf');
            
    header('Content-Length: ' filesize($file));
            
            
    header('Content-Disposition: inline; filename=' basename($file));
            
    // header('Content-Disposition: attachment; filename=' . basename($file));
            
    readfile($file);/
    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    By echo'ing the contents before the error's, you're sending the headers before your header() statement by default. Headers are sent BEFORE output, so having output before produces an error (which you would see if error reporting is on).

    Remove that echo and everything will work - look up header() in the php manual and that will tell you more.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    washington, dc
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I did so and still get an error. The image is attached when I run the script. Here is the new version of the script. By the way, thanks for that catch, one step closer!

    PHP Code:
    <?php

         $ch 
    curl_init();
         
    curl_setopt($chCURLOPT_URL'http://www.mainwebsitelink.com/Download.do?action=download&dataId=55555&format=title_of_the_file_i_want');
         
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
         
    $contents curl_exec ($ch);
         
    curl_close ($ch);
         
         
    $file $contents;

            
    header('Content-Description: File Transfer');
            
    header('Content-Type: application/pdf');
            
    header('Content-Length: ' filesize($file));
            
            
    header('Content-Disposition: inline; filename=' basename($file));
            
    // header('Content-Disposition: attachment; filename=' . basename($file));
            
    readfile($file);
    ?>
    I should mention that when I use the code below that if I do a "save page as" and I finish it off with a .pdf, the file is perfect! I don't know if that helps this issue or not but wanted to mention, here is that code:
    PHP Code:
    <?php

         $ch 
    curl_init();
         
    curl_setopt($chCURLOPT_URL'http://www.getabstract.com/Download.do?action=download&dataId=15054&format=1&u=us_department_of_veterans_affairs');
         
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
         
    $contents curl_exec ($ch);
         
    curl_close ($ch);
         echo 
    $contents;
    ?>
    Attached Thumbnails Attached Thumbnails header() not allowing me to download the format-error.jpg  

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Edit: Wow, that was some terrible English. What I meant to say was, you're using $contents as a filename but it looks more like it's the actual pdf contents. Is $contents a filename/location, or pdf contents?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    New Coder
    Join Date
    Sep 2011
    Location
    washington, dc
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts
    No problem... Thanks again for the help! It is pdf content...

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $contents isn't a file, you cannot expect to get the data using a readfile.
    First specify the filename within the content-disposition. There is no "basename" of the string you have provided. Since $content is already the data you need, you simply print it.

  • #7
    New Coder
    Join Date
    Sep 2011
    Location
    washington, dc
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    $contents isn't a file, you cannot expect to get the data using a readfile.
    First specify the filename within the content-disposition. There is no "basename" of the string you have provided. Since $content is already the data you need, you simply print it.
    Thanks for the "basename" issue, I didn't even think of that. And when I print it / echo it back, I get the file in bit but I instead want it to save itself out as a PDF. Right now, I have to go to save page as and change the extension to ".pdf" to have it save correctly when I do the code above two posts.

    How would you print it out as a pdf?

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    First you need to know the mime type so code similar to this would help:
    PHP Code:
       switch($file_extension)
          {
          case 
    'exe'$ctype='application/octet-stream'; break;
          case 
    'zip'$ctype='application/zip'; break;
          case 
    'mp3'$ctype='audio/mpeg'; break;
          case 
    'mpg'$ctype='video/mpeg'; break;
          case 
    'avi'$ctype='video/x-msvideo'; break;
          case 
    'rar'$ctype='application/rar'; break;
          default:    
    $ctype='application/force-download';
          } 
    Then you need to supply some details to the browser such as the length, the mime/content type and the attachments filename:

    PHP Code:
       header('Content-Type: ' $ctype);
       
    header('Content-Disposition: attachment; filename="' $filename '"');
       
    header('Content-Length: ' .$FileLength); 
    Note that (as demonstrated in one of the links in my signature) any call to header() must be BEFORE any other content is put out to the browser.
    I can't really think of anything to write here now...

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by SimpleNE View Post
    How would you print it out as a pdf?
    print $contents;.
    You've already told the browser that you are presenting the data and want it provided as an attachment. The browser should prompt the user for a location to save the document.


  •  

    Tags for this Thread

    Posting Permissions

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