Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2006
    Posts
    17

    Unanswered: PHP header(content-disposition...) Help

    Hi everyone..

    I seem to be having a strange issue.. This piece of code works great in my test server.. However, when I moved it out to my production server it does not like how I added the PHP var on the filename... Does anyone know why?


    PHP Code:
    $strDate $_REQUEST["date"];
            
    header("Content-Type: application/vnd.ms-excel");
            
    header("Expires: 0");
            
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            
    header("content-disposition: attachment;filename=filename_$strDate.xls"); 
    Thanks

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Because you are using "" (double quotes) PHP thinks that filename=filename_$strDate.xls should be written out as just that. Also the . (dot) that appears in the code will probably be misinterpreted too. Is it part of the variable or not, and should it concatenate $strDate with xls (thus giving e.g. 2007-01-02xls [NOTICE THE MISSING DOT?]) ? What you need to do is replace it with the variable correctly.

    so

    Code:
    header("content-disposition: attachment;filename=filename_{$strDate}.xls");
    I expect you'll probably find that the versions you are using between testing and production as different...

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Oops better correct myself here. '' (single inverted commas) actually causes '$variables' to be written out as $variables

  4. #4
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    Define - "it does not like how I added the PHP var on the filename" What exactly is it doing?

    We only see the information that you provide and telling us accurate and descriptive information about what exactly the symptoms are is necessary for anyone to offer anything more than guesses about what the problem is.

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Indeed an error message might help us a little bit.

    Also, please test this and let us know what result you get :

    Code:
    $strDate = $_REQUEST["date"]; 
    echo "content-disposition: attachment;filename=filename_$strDate.xls"

  6. #6
    Join Date
    Aug 2006
    Posts
    17
    Sorry, I should of been more specific.. It does not give me an error, instead it names the file the name of the .php file instead of the name i created..
    I will test the

    PHP Code:
    $strDate $_REQUEST["date"]; 
    echo 
    "content-disposition: attachment;filename=filename_$strDate.xls" 
    and let you know.

    Thanks

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by mattv10
    Sorry, I should of been more specific.. It does not give me an error, instead it names the file the name of the .php file instead of the name i created..
    I will test the

    PHP Code:
    $strDate $_REQUEST["date"]; 
    echo 
    "content-disposition: attachment;filename=filename_$strDate.xls" 
    and let you know.

    Thanks
    ..in that case Id try specifically casting the variable
    PHP Code:
    $strDate $_REQUEST["date"]; 
    echo 
    "content-disposition: attachment;filename=filename_".$strDate.xls
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    Upon further review of the syntax, it is not correct. Example from the PHP manual -
    PHP Code:
    header('Content-Disposition: attachment; filename="downloaded.pdf"'); 
    You need quotes around the filename portion as well as around the whole header parameter.

    The following should work -
    PHP Code:
    header("Content-Disposition: attachment; filename='filename_{$strDate}.xls'"); 

  9. #9
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Healdem : Just to quickly clarify something you did in your example :

    Code:
    $strDate = $_REQUEST["date"]; 
    echo "content-disposition: attachment;filename=filename_".$strDate.xls;
    the last part of that will not work as expected. PHP will attempt to interpret .xls as the concatenation of a constant called xls. Of course, that constant doesn't exist so I would expect php to throw an error or best guess it (probably make it null or '' or 0) and thus concatenate that to the end.

    The correct way would have been :
    Code:
    $strDate = $_REQUEST["date"]; 
    echo "content-disposition: attachment;filename=filename_".$strDate.".xls";
    However we now know that dbmab's answer is correct.

    Edit : Ran healdem's code through a PHP compiler and it actually inteprets it as a string constant 'xls'. It does throw an error : Use of undefined constant xls - assumed 'xls'.
    Just so no-one else makes this mistake, never assume that it will get intepreted as the string 'xls'...
    Last edited by aschk; 04-26-07 at 09:42.

  10. #10
    Join Date
    Aug 2006
    Posts
    17
    Upon testing dbmab's answer it still did not work... However, I think I found out why... I used aschk suggestion and did this:

    PHP Code:
    $strDate $_REQUEST["date"]; 
    echo 
    "content-disposition: attachment;filename=filename_$strDate.xls" 
    This was my output:
    filename_4/12/2007.xls... Obviously that is why its not printing correctly.. For some strange reason on my test server it converts the /'s to _'s i don't know why or how... This is where I need some assistance.. Should I just pass the date through my URL using 4_12_2007 rather than 4/12/2007? Or is there an alternative method?

    Thanks,


    Well I solved the issue... Finally! Thanks for all your help everyone. Here is what I did..
    PHP Code:
                    $sDate $_REQUEST["date"];
            
    $strDate=str_replace('/'"_"$sDate);
            
    header("Content-Type: application/vnd.ms-excel");
            
    header("Expires: 0");
            
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            
    header("Content-Disposition: attachment;filename=filename_{$strDate}.xls"); 
    Last edited by mattv10; 04-26-07 at 14:31.

  11. #11
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    How you choose to pass the date data is entirely up to yourself of course.
    Are you taking that from an input box? i.e.
    type in 14/02/2004 to get the filename with that on the end?
    Or are you automagically outputting links to each file name that people can click on?

    If you are doing the former then you have no real option other than to convert the data into the correct "_" format when you pass the request on.

    If however you are doing the latter then it's very much a case of efficiency versus practicality. For example :
    each time you write out the page do you want to convert the dates into their underscore versions?
    or
    do you want to parse the dates that get requested each time someone actually clicks the file.

    The more efficienct from a system load point of view is the latter of those examples. You only process the str_replace for the filename selected, whereas the former example means each time you view the page you have to perform the str_replace on output for every filename (even if they won't be clicked on).

    It's very much up to you and how your data is received.

  12. #12
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so why not reformat the date to say YYYYMMDD using the appropriate date mask. Id suggest the YYYYMMDD mask so that the file names sorts in ascending date order (yes I know most users are able to sort in date order... but)

    Id guess that your test server has magic quotes switched on, whereas the live server may have it turned off. Ie the / character is being escaped
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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