Asked  7 Months ago    Answers:  5   Viewed   28 times

What is the best possible way to detect if a download is completed, because afterward I want to update the database.

I tried some of this code from the PHP manual, but it doesn't do much for me:

header("Content-Type: application/octet-stream"); 
header("Content-Length: ".filesize($file)); 
header("Content-Disposition: attachment; filename=$filename");

// buffered read not using readfile($file);

if ($fp = fopen($bestand, 'rb')) {
    while (!feof($fp)) {
        $buf = fread($fp, 4096);
        echo $buf;
        $bytesSent += strlen($buf);    /* We know how many bytes were sent to the user */

if ($bytesSent == filesize($fp)) {
    //do something with db



That's not really going to tell you if the download has completed for the user. It will tell you when you have finished sending bytes to the user, but it says nothing about how many of them the user has actually received.

In reality, there is no way with PHP (which is a server-side, not a client-side, language) to truly detect when a file download has completed. The best you can do is log the download in your database when it begins. If you absolutely, completely and totally need to know when the download has completed, you'll have to do something like embed a Java applet or use Flash. However, usually that is not the correct answer in terms of usability for your user (why require them to have Java or Flash installed just to download something from you?).

Wednesday, March 31, 2021
answered 7 Months ago

I found the error:

 $dimension_file=(string)filesize($name_file); //<-- HERE! --- 

i was retrieving the size using only the name of file instead of using the full path

filesize($name_file) ---> filesize( $path . $name_file) 

the error was hidden from

header("Content-Type: application/"); 

and the php error response added to the content of the downloaded file.

So i suggest to who has this kind of problems to comment the "Content-Type" while debugging to see if there are some errors in the php code and when all code seems to work re-enable the "Content-Type" header.

On my server spaces in the code before the


has no influence on the checksum

Thanks to Vladimir and Rocket for good practice tips

Wednesday, March 31, 2021
answered 7 Months ago

IDs must be unique in your document, meaning that you shouldn't do this:

<input type="checkbox" name="chk[]" id="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" id="chk[]" value="Bananas" />

Instead, drop the ID, and then select them by name, or by a containing element:

<fieldset id="checkArray">
    <input type="checkbox" name="chk[]" value="Apples" />

    <input type="checkbox" name="chk[]" value="Bananas" />

And now the jQuery:

var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
//there should be no space between identifier and selector

// or, without the container:

var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;
Tuesday, June 1, 2021
answered 5 Months ago

add target=_blank in your ajax success function like below

success: function(){'http://YOUR_URL','_blank' );

otherwise you can handle smartly to open your Excel download link in new tab with jQuery trigger function or etc.

Monday, July 19, 2021
answered 3 Months ago

{{}} does string interpolation and stringifies true and false and Angular by default uses property binding and I assume the property expects boolean values not strings:

<input type="checkbox" [checked]="person.is_adult">

This might work as well

<input type="checkbox" attr.checked="{{person.is_adult}}">

because with attribute binding the browser might translate it from the attribute (which can only be strings) to boolean when reading it into its property.

It is also checked instead of value

You can also use ngModel

<input type="checkbox" [ngModel]"person.is_adult" name="isAdult">
<input type="checkbox" [(ngModel)]"person.is_adult" name="isAdult">

for one-way or two-way binding.
Ensure your have the FormsModule imported if you use ngModel.

Friday, August 13, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :