Solved

Send long string as aparameter

Posted on 2004-09-28
7
283 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

816 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

12 Experts available now in Live!

Get 1:1 Help Now