473,666 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete file after headers sent


Hi guys,

Is there any way how to delete a file after download headers have been
sent? This code does not work for some reason.

header('Content-type: application/xml');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.filesize($f)) ;
header('Content-Disposition: attachment; filename="'.bas ename($f).'"');

if($fp = fopen($f, "rb"))
{
while ($buffer = fread($fp, filesize($f))) print $buffer;
fclose($fp);
}

unlink($f);
--
Collector
------------------------------------------------------------------------
Collector's Profile: http://techiegroups.com/member.php?userid=264
View this thread: http://www.techiegroups.com/showthread.php?t=115692

Sep 1 '06 #1
5 5158
You may need to CHMOD the file (and possibly the containing folder) to
be writable by the script.

Collector wrote:
Hi guys,

Is there any way how to delete a file after download headers have been
sent? This code does not work for some reason.

header('Content-type: application/xml');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.filesize($f)) ;
header('Content-Disposition: attachment; filename="'.bas ename($f).'"');

if($fp = fopen($f, "rb"))
{
while ($buffer = fread($fp, filesize($f))) print $buffer;
fclose($fp);
}

unlink($f);
--
Collector
------------------------------------------------------------------------
Collector's Profile: http://techiegroups.com/member.php?userid=264
View this thread: http://www.techiegroups.com/showthread.php?t=115692
Sep 1 '06 #2
Collector wrote:
>
Hi guys,

Is there any way how to delete a file after download headers have been
sent? This code does not work for some reason.

header('Content-type: application/xml');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.filesize($f)) ;
header('Content-Disposition: attachment; filename="'.bas ename($f).'"');

if($fp = fopen($f, "rb"))
{
while ($buffer = fread($fp, filesize($f))) print $buffer;
fclose($fp);
}

unlink($f);

Can you unlink the file anyway?
I seriously doubt the headers are influencing this...

Regards,
Erwin Moller
Sep 1 '06 #3

Thanks guys. Nope, it has nothing to do with permissions. I am able to
unlink the file with no problem. After sending download headers it
looks like PHP can execute only the next line (reading and sending out
the file) but nothing else. I was just wondering if there is some
workaround leading to successful deletion of a file after headers of
this type have been sent. Thanks.
--
Collector
------------------------------------------------------------------------
Collector's Profile: http://techiegroups.com/member.php?userid=264
View this thread: http://www.techiegroups.com/showthread.php?t=115692

Sep 1 '06 #4
Collector wrote:
Thanks guys. Nope, it has nothing to do with permissions. I am able to
unlink the file with no problem. After sending download headers it
looks like PHP can execute only the next line (reading and sending out
the file) but nothing else. I was just wondering if there is some
workaround leading to successful deletion of a file after headers of
this type have been sent. Thanks.

As Erwin indicated - headers should have nothing to do with this. You
should be able to unlink the file with no trouble.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 1 '06 #5
*** Collector escribió/wrote (Fri, 1 Sep 2006 02:49:22 -0400):
while ($buffer = fread($fp, filesize($f))) print $buffer;
Depending of the size of your file, this can make your script run our of
memory and crash, so you after that won't be able to remove the file... or
print "hello word". Try readfile() or, simply don't use the full file size
as buffer size. Actually, I can't see the point of a while loop that's
supposed to be run once.

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
21055
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use. What the code does: * First writes "This is sent to prompt" to prompt.
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
16
17004
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
1
5378
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved completely/succsessfully on the client's computer before updating some metadata on the server (file downloaded date for instance) However, All examples i have tried, and all examples I have found that other people says works - doesn't work for me :-(
9
1510
by: comp.lang.php | last post by:
<?php if (headers_sent()) print_r('headers sent'); // do other stuff ?> I have a file, "classes.inc.php", a library of PHP version 4+ classes. This library file, the moment it is included, causes headers to be generated into the HTTP headers! I have no explanation as to how it does that, it just does that! I have tried everything I can think of,
4
1761
by: Brad | last post by:
I have a file that downloaded a CSV file for the user based upon some information gathered from the Database. My file was working fine until recently (I believe that my hosting company did something when they upgraded to PHP5). Everytime a user tries to download the file, it displays the contents on the screen. Here is what I have for my file handling. Am I missing anything obvious? when called, $filecontent is displayed to the...
6
7360
by: john | last post by:
The standard method to transmit a file from an aspx page to a browser is to stream the file to the response then end the response. The HTML code generated by the aspx page is discarded, and the browser displays or offers to save the binary file instead. I would like to have the browser accept and display the revised HTML code as well as offering to open or save the attached file. This SHOULD be possible using a multipart MIME format -...
0
937
by: bruce | last post by:
hi jackie, if you don't mind... can i ask what you're looking to accomplish? are you looking to simply get the text/string data, or something else??? -----Original Message----- From: python-list-bounces+bedouglas=earthlink.net@python.org On Behalf
1
1348
by: DavidPr | last post by:
I'm using the delete script below. It's deleting and sending the email OK, but the correct information is not being sent. I tested this script on one database and it deleted several ads, but the email I received said that "No ads were deleted." How do I fix this? delete_old.php:<?php include("dbconnect.php"); $query = "DELETE FROM ads WHERE submitted < SUBDATE(NOW(), INTERVAL 45 DAY)"; $result = mysql_query($query); if...
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
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.