468,252 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,252 developers. It's quick & easy.

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="'.basename($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 4939
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="'.basename($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="'.basename($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*******@attglobal.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Omid | last post: by
16 posts views Thread by Philip Boonzaaier | last post: by
9 posts views Thread by comp.lang.php | last post: by
4 posts views Thread by Brad | last post: by
6 posts views Thread by john | last post: by
1 post views Thread by DavidPr | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.