473,507 Members | 9,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to generate a text file for download from memory

Claus Mygind
571 Contributor
The online php manual provides this code for downloading a file
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $file = 'monkey.gif';
  4.  
  5. if (file_exists($file)) {
  6.     header('Content-Description: File Transfer');
  7.     header('Content-Type: application/octet-stream');
  8.     header('Content-Disposition: attachment; filename='.basename($file));
  9.     header('Expires: 0');
  10.     header('Cache-Control: must-revalidate');
  11.     header('Pragma: public');
  12.     header('Content-Length: ' . filesize($file));
  13.     readfile($file);
  14.     exit;
  15. }
  16. ?>
  17.  
This requires that I have a file somewhere I want to download.

If I have the data stored in memory, can I generate the download file directly from that data? Or must I first write some temp file on the server so I can output the text file for download?

I have code for the 2nd option here, just wondering if option 1 is possible, if so do you have some sample code to demonstrate?
Nov 20 '14 #1
3 1579
Rabbit
12,516 Recognized Expert Moderator MVP
Instead of readfile, echo whichever variable is holding the file data in memory.
Nov 20 '14 #2
Claus Mygind
571 Contributor
file is used in 2 different places. Note it is also used for content length.

header('Content-Length: ' . filesize($file));

Would say this line is optional, even though my file will be slightly different each time the app is run?

Also the data has to be strung together in a loop to complete the file with a line return after each record.
Nov 20 '14 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Instead of the filesize, you can calculate it since you have access to the variable. The size or length of the variable will replace the filesize.

I don't know if the filesize is optional or not, you can try dropping it.
Nov 20 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6081
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
8
18772
by: Rune Johansen | last post by:
Hi, I'm sorry if these questions are trivial, but I've searched the net and haven't had any luck finding the information I need. I need to perform some regular expression search and replace on...
4
10815
by: Nicholas Then | last post by:
I am writing an ASP.NET application and I have a class that I have written to create a vCard...it just returns a string with all the necessarry info... Anyway...is there a way that I can create a...
2
1987
by: Tom Youngquist | last post by:
I am trying to download a text file that my .NET page has just created based on entered parameters on the web page. Everything seems to work and the file is created. I am using the following code...
4
3087
by: Gary | last post by:
Hello, I am using asp classical and connection to SQL 2000 on a Windows 2000 machine. I am using this code to generate a text file when the user clicks on the link. This is so the user can...
2
2088
by: Peter | last post by:
Hi! Please, could anybody advice me, is it possible to disable file download dialog in ASP.NET page or just save button in the file download dialog so that user should only open for example...
3
1701
by: fabian | last post by:
Hi, I've got this code: Dim fo As File Dim swriter As StreamWriter swriter = fo.CreateText("c:\prueba.txt") swriter.Write("this is a test") swriter.Close()
5
12270
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
1
8517
by: R.A.M. | last post by:
Hello, I am learning PHP5. I have little experience. I have created a website in which I want to put a link to download a text file. When I used simple <a href="Dir/File.txt">Download file</a> I...
1
47347
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.