Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 标头函数文件名在PHP中不起作用_Javascript_Php_Mysql - Fatal编程技术网

Javascript 标头函数文件名在PHP中不起作用

Javascript 标头函数文件名在PHP中不起作用,javascript,php,mysql,Javascript,Php,Mysql,标头函数文件名在PHP中不起作用。我尝试导出CSV文件,但它总是像export.php一样下载页面名称 我尝试了很多代码并强制下载。但我不能。有人能帮帮我吗 enter code here if(isset($_POST["export"])) { include 'database/config.php'; include "database/database.php"; $db = new database(); $fn = "csv_".uniqid()

标头函数文件名在PHP中不起作用。我尝试导出CSV文件,但它总是像export.php一样下载页面名称 我尝试了很多代码并强制下载。但我不能。有人能帮帮我吗

enter code here
if(isset($_POST["export"]))
{    include 'database/config.php';
     include "database/database.php";
     $db = new database();
     $fn = "csv_".uniqid().".csv";

     $file = fopen($fn, "w");
     $query = "SELECT * from wp_terms";
     $read = $db -> select($query);
     fputcsv($file, array('ID', 'Name', 'slug', 'term group'));
     if($read) {
        while ($row = $read->fetch_assoc()) {
          fputcsv($file, $row);
        }
      }
     header('Content-Type: text/csv; charset=utf-8');
     header('Content-Disposition: attachment; filename="'.$fn);

     fclose($file);
   }

这将非常适合我,所以请尝试以下代码

add_action("admin_init", "download_csv");

function download_csv() {

  if (isset($_POST['download_csv'])) {

    global $wpdb;

    $sql = "SELECT `sub_email` FROM `wp_terms`";

    $rows = $wpdb->get_results($sql, 'ARRAY_A');

    if ($rows) {

        $csv_fields = array();
        $csv_fields[] = "first_column";
        $csv_fields[] = 'second_column';
        $current_date = date("Y-m-d");

        $output_filename = 'subscriber_list'.$current_date.'.csv';
        $output_handle = @fopen('php://output', 'w');

        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Description: File Transfer');
        header('Content-type: text/csv');
        header('Content-Disposition: attachment; filename=' . 
        $output_filename);
        header('Expires: 0');
        header('Pragma: public');

        $first = true;
       // Parse results to csv format
        foreach ($rows as $row) {

       // Add table headers
            if ($first) {

               $titles = array();

                foreach ($row as $key => $val) {

                    $titles[] = $key;

                }

                fputcsv($output_handle, $titles);

                $first = false;
            }

            $leadArray = (array) $row; // Cast the Object to an array
            // Add row to file
            fputcsv($output_handle, $leadArray);
        }

        //echo '<a href="'.$output_handle.'">test</a>';

        // Close output file stream
        fclose($output_handle);

        die();
    }
  }
}
add_action(“admin_init”,“download_csv”);
函数下载_csv(){
如果(isset($\u POST['download\u csv'])){
全球$wpdb;
$sql=“从'wp_terms'中选择'sub_email'”;
$rows=$wpdb->get_results($sql,'ARRAY_A');
如果($行){
$csv_字段=数组();
$csv_字段[]=“第一列”;
$csv_字段[]=“第二列”;
$current_date=日期(“Y-m-d”);
$output_filename='subscriber_list'。$current_date..csv';
$output\u handle=@fopen('php://output","w",;
标头('Cache-Control:必须重新验证,后检查=0,前检查=0');
标题(“内容描述:文件传输”);
标题(“内容类型:文本/csv”);
标题('Content-Disposition:attachment;filename=')。
$output_filename);
标题('Expires:0');
标题(“Pragma:public”);
$first=true;
//将结果解析为csv格式
foreach($行作为$行){
//添加表格标题
如果($第一){
$titles=array();
foreach($key=>$val的行){
$titles[]=$key;
}
fputcsv($output\u handle,$titles);
$first=false;
}
$leadArray=(array)$row;//将对象强制转换为数组
//将行添加到文件
fputcsv($output\u handle,$leadArray);
}
//回声';
//关闭输出文件流
fclose($output\u handle);
模具();
}
}
}

您是否检查了标题是否正确,或者在
标题中是否缺少一些引号('Content-Disposition:attachment;filename=“”。$fn);
?你使用wordpress对吗?@kerbholz是的,有一些引号问题。我更正了它们,但仍然没有working@Dhruv我只使用wordpress数据库。我使用了所有的头函数,但它仍然作为.php文件下载。如果我删除了所有头函数,那么它将作为csv文件下载到htdocs文件夹中。