Delete file after headers sent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Collector

    #1

    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

  • Frozen

    #2
    Re: Delete file after headers sent

    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

    Comment

    • Erwin Moller

      #3
      Re: Delete file after headers sent

      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

      Comment

      • Collector

        #4
        Re: Delete file after headers sent


        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

        Comment

        • Jerry Stuckle

          #5
          Re: Delete file after headers sent

          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.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Alvaro G. Vicario

            #6
            Re: Delete file after headers sent

            *** 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
            --

            Comment

            Working...