File download time in PHP/MySQL

后端 未结 3 368
借酒劲吻你
借酒劲吻你 2021-01-29 15:59

I want to find download time in PHP by sending a request to my MySQL server. I have one table with an href link to download the file. If I click download the download time shoul

相关标签:
3条回答
  • 2021-01-29 16:23

    I am not very much sure, but I don't think it is possible via PHP.

    When I open a PHP page, the webserver gives me the page. When I click on a link which say downloads xyz.abc file, then it is actually another request to the webserver and the current PHP page is not involved in this. The current PHP page is just hosting the link.

    When I click on the link, the webserver finds the file and sends it to the client. It is the webserver which serves the page and it is the webserver which can keep track of the time since it knows when it has opened the socket for file download and when that socket got closed.

    If anyone knows how to do it via a PHP script, please let me know too.

    0 讨论(0)
  • 2021-01-29 16:29

    Ok, now I understand the question based on OP's comment. The question is how to figure out what time did a user downloaded a file. If that's the case, the download link needs to be a php script, and it would write in the time into db, then returns the content of the file into the stream with proper content header.

    See readfile.

    <?php
    $file = 'monkey.gif';
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    ?>
    

    All you have to do is pass in the file name as some parameter and write the current time into DB.

    0 讨论(0)
  • 2021-01-29 16:39

    if you need some code you should use filesize and you can evaluate the time to download by doing some calculation, you cannot know the exact time since it depend on the user speed and all the network between your server and the user.

    $filesize = filesize($yourfile);
    $time_for_modem = $filesize * 8 / (56*1024);
    $time_for_adsl = $filesize * 8 / (5*1024*1024);
    $time_for_t3 = $filesize * 8 / (44*1024*1024);
    
    function convertSecondToTime($sec){
      $hour = floor($sec/60)
      return $hour . 'hours and ' . ($sec%60) . 'minutes';
    }
    

    The modem time is for a 56kbps connection, adsl a 5 mbps, T3 44 mbps connection.

    0 讨论(0)
提交回复
热议问题