By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

Delete file after headers sent

P: n/a

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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
*** 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.