0
<?php
// force to download a file
if(isset($_POST['download'])){
    $file = "images/dansyo_logo.png";
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header( "Content-Disposition: attachment; filename=".basename($file));
    header( "Content-Description: File Transfer");
    @readfile($file);
    if(@readfile($file)){
        echo'proceed';
    }else{
        echo'failded';
    }
}
?>

<form method='post'>
    <button name='download'>download</button>
</form>

I have the above code where i want the code to be able to download a file and in the same time insert values into my database.The code is functioning well that is i'm able to download a file.However,nothing is echoed after the file has downloaded or is downloading.

1
  • do whatever you want before header send. Nov 19, 2014 at 9:57

1 Answer 1

1

Its not going to echo since header will send the file to browser for download, if you wanted to do any kind of db submission just do it before header sent

$file = "images/dansyo_logo.png";

//Do your DB update here, before the header
if(is_readable($file)) {
  // insert into database 
} else {
    exit(basename($file)." not found.");
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Description: File Transfer");
@readfile($file);
exit();

Note: file_exists() will return true on a directory.

4
  • hi Sanqueib,i have tried your concept but it is giving me this error,call to undefined function
    – user3824154
    Nov 19, 2014 at 10:00
  • try updated code, it should work, its working on my pc
    – Saqueib
    Nov 19, 2014 at 10:05
  • Thanks for concern,and another question,i am trying to echo a value if the file is readable or not but is not giving me any result.What would be problem with the code
    – user3824154
    Nov 19, 2014 at 10:10
  • if file is readable it will start downloading, if not you can just use exit( basename($file) . ' not found.' ); updated the answer
    – Saqueib
    Nov 19, 2014 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.