Solved

Send long string as aparameter

Posted on 2004-09-28
7
264 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 100 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now