下载文件并将值插入数据库

时间:2014-11-19 09:46:32

标签: php

<?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>

我有上面的代码,我希望代码能够下载文件,同时将值插入我的数据库。代码运行良好,我可以下载文件。但是,文件下载或下载后没有任何回应。

1 个答案:

答案 0 :(得分:1)

它不会回显,因为标题会将文件发送到浏览器进行下载,如果你想做任何类型的db提交,只需在标题发送前执行

$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();

注意:file_exists()将在目录中返回true。