How to detect if file download completed or cancelled

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

    #1

    How to detect if file download completed or cancelled

    I have a large install file (an exe) on my web server that people
    download and install from. Looking at my log files, I see a lot of
    people downloading it, but no way to tell for sure if they completed the
    download or cancelled out before it completed. Is there any function in
    PHP that would allow the web server to send the file and detect a
    completion or cancellation? Or perpahs a javascript/PHP method?

    Any help would be greatly appreciated.
    Thanks,
    Dan
  • petersprc@gmail.com

    #2
    Re: How to detect if file download completed or cancelled

    You could check the byte count in the web server log to see if the
    whole file was transferred.

    In PHP, you could also do this by checking connection_abor ted() after
    sending the file. For example:

    ignore_user_abo rt(true); // Don't end if the connection breaks

    if (readfile($path ) !== false && !connection_abo rted()) {
    // Success!
    }

    Here's a larger example. The function sendFile returns the status of
    the download:

    <?

    // Sample usage

    function sendTest()
    {
    $res = sendFile('appli cation.exe', 'application/octet-stream');

    if ($res['status']) {
    // Download succeeded
    } else {
    // Download failed
    }

    @saveDownloadSt atus($res);
    }

    // The sendFile function streams the file and checks if the
    // connection was aborted.

    function sendFile($path, $contentType = 'application/octet-stream')
    {
    ignore_user_abo rt(true);

    header('Content-Transfer-Encoding: binary');
    header('Content-Disposition: attachment; filename="' .
    basename($path) . "\";");
    header("Content-Type: $contentType");

    $res = array(
    'status' =false,
    'errors' =array(),
    'readfileStatus ' =null,
    'aborted' =false
    );

    $res['readfileStatus '] = readfile($path) ;
    if ($res['readfileStatus '] === false) {
    $res['errors'][] = 'readfile failed.';
    $res['status'] = false;
    }

    if (connection_abo rted()) {
    $res['errors'][] = 'Connection aborted.';
    $res['aborted'] = true;
    $res['status'] = false;
    }

    return $res;
    }

    // Save the status of the download to some place

    function saveDownloadSta tus($res)
    {
    $ok = false;
    $fh = fopen('download-status-' . $_SERVER['REMOTE_ADDR'] . '-' .
    date('Ymd_His') , 'w');
    if ($fh) {
    $ok = true;
    if (!fwrite($fh, var_export($res , true))) {
    $ok = false;
    }
    if (!fclose($fh)) {
    $ok = false;
    }
    }
    return $ok;
    }

    Dan D wrote:
    I have a large install file (an exe) on my web server that people
    download and install from. Looking at my log files, I see a lot of
    people downloading it, but no way to tell for sure if they completed the
    download or cancelled out before it completed. Is there any function in
    PHP that would allow the web server to send the file and detect a
    completion or cancellation? Or perpahs a javascript/PHP method?
    >
    Any help would be greatly appreciated.
    Thanks,
    Dan

    Comment

    • Dan D

      #3
      Re: How to detect if file download completed or cancelled

      Thank you very much. That looks like it should work great. I'll give it
      a try.
      Dan

      petersprc@gmail .com wrote:
      You could check the byte count in the web server log to see if the
      whole file was transferred.
      >
      In PHP, you could also do this by checking connection_abor ted() after
      sending the file. For example:
      >
      ignore_user_abo rt(true); // Don't end if the connection breaks
      >
      if (readfile($path ) !== false && !connection_abo rted()) {
      // Success!
      }
      >
      Here's a larger example. The function sendFile returns the status of
      the download:
      >
      <?
      >
      // Sample usage
      >
      function sendTest()
      {
      $res = sendFile('appli cation.exe', 'application/octet-stream');
      >
      if ($res['status']) {
      // Download succeeded
      } else {
      // Download failed
      }
      >
      @saveDownloadSt atus($res);
      }
      >
      // The sendFile function streams the file and checks if the
      // connection was aborted.
      >
      function sendFile($path, $contentType = 'application/octet-stream')
      {
      ignore_user_abo rt(true);
      >
      header('Content-Transfer-Encoding: binary');
      header('Content-Disposition: attachment; filename="' .
      basename($path) . "\";");
      header("Content-Type: $contentType");
      >
      $res = array(
      'status' =false,
      'errors' =array(),
      'readfileStatus ' =null,
      'aborted' =false
      );
      >
      $res['readfileStatus '] = readfile($path) ;
      if ($res['readfileStatus '] === false) {
      $res['errors'][] = 'readfile failed.';
      $res['status'] = false;
      }
      >
      if (connection_abo rted()) {
      $res['errors'][] = 'Connection aborted.';
      $res['aborted'] = true;
      $res['status'] = false;
      }
      >
      return $res;
      }
      >
      // Save the status of the download to some place
      >
      function saveDownloadSta tus($res)
      {
      $ok = false;
      $fh = fopen('download-status-' . $_SERVER['REMOTE_ADDR'] . '-' .
      date('Ymd_His') , 'w');
      if ($fh) {
      $ok = true;
      if (!fwrite($fh, var_export($res , true))) {
      $ok = false;
      }
      if (!fclose($fh)) {
      $ok = false;
      }
      }
      return $ok;
      }
      >
      Dan D wrote:
      >
      >>I have a large install file (an exe) on my web server that people
      >>download and install from. Looking at my log files, I see a lot of
      >>people downloading it, but no way to tell for sure if they completed the
      >>download or cancelled out before it completed. Is there any function in
      >>PHP that would allow the web server to send the file and detect a
      >>completion or cancellation? Or perpahs a javascript/PHP method?
      >>
      >>Any help would be greatly appreciated.
      >>Thanks,
      >>Dan
      >
      >

      Comment

      Working...