Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Send long string as aparameter

Posted on 2004-09-28
7
Medium Priority
?
327 Views
Last Modified: 2012-06-21
In my php/html application, I need to let the user download an xls file I have stored in $xls string.

I thought to do this way:

if (isset($_GET['download']) && $_GET['download']==true) {
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=extraction.xls");
            header("Pragma: no-cache");
            header("Expires: 0");
            print "$header\n$xls";
            }
            
      echo "<a href=\"{$_SERVER["PHP_SELF"]}?download=1\">Download table</a>";

but it doesn't work beacuse headers are already set to the page. If I put the code into another page, I have the problem how to send the variable $xls to the other page?? (The variable is long and contains several \n and \t characters...)
0
Comment
Question by:winmeister
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
php-webdesign earned 300 total points
ID: 12168473
you can try writing it to a txt file fput() and in the following page read it from the file fget()
0
 
LVL 1

Author Comment

by:winmeister
ID: 12168588
yes, but how can I then change to the "send" page without incurring in the: "Cannot modify header information" error?
0
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12168630
in the current page you insert:

<?php
$filename = 'xls.txt';

if (is_writable($filename)) {

   if (!$handle = fopen($filename, 'w')) {
         print "Can't open file ($filename)";
         exit;
   }

   if (!fwrite($handle, $header\n$xls)) {
       print "Can't write to ($filename)";
       exit;
   }

   print "Succes, written the content to file ($filename)";

   fclose($handle);

} else {
   print "The file isn't writable... chmod 777?";
}
?>

This puts the content into the file

now create a new file

download.php

<?php
$filename = "xls.txt";
$handle = fopen ($filename, "rb");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);

          header("Content-type: application/octet-stream");
          header("Content-Disposition: attachment; filename=extraction.xls");
          header("Pragma: no-cache");
          header("Expires: 0");
          print "$contents";
          }
?>

something like this?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:winmeister
ID: 12168693
Oh yes. But I don't need always to save the file. So when the user clicks on a link on page number 1, the program should:

1) save the file
2) call page #2 for downloading
3) come back to the original page

..here is where I'm getting lost.
0
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12168713
well... now the page does:
1) save the file
2) starts the download

and if you add

header("Location: originalpage.php");

to the script, it redirects him to the originalpage.php, but be sure to add this line at the end of your download.php page...

or do you mean something else?
0
 
LVL 1

Author Comment

by:winmeister
ID: 12168789
no, if I add
header ("Location.....
on page #1 I am getting a "Cannot modify header information" error?

More, I have seen different behaviours between Mozilla-Firefox and IE 6.
IE6 dosen't get the filename and type, so it won't download...!
0
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12168804
the Header("Location is for download.php then :)

and for IE en Firefox... sorry can't help you with them
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question