Asked  2 Years ago    Answers:  5   Viewed   48 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?).

Thursday, December 1, 2022

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;
Saturday, November 12, 2022

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.

Saturday, December 10, 2022

{{}} 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.

Saturday, September 17, 2022

just use the builtin functionality as follows

$phpWord->save('teste.docx', 'Word2007', true);

The last parameter will force a download of the produced file.

Wednesday, September 28, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages