0

我有一些将 mysql 数据库导出到 .xls 文件的 php 代码。我可以在运行代码时立即下载。但是,每次以下代码生成新的 .xls 文件时,我可以使用 php 将此文件发送到管理员电子邮件帐户吗?

谢谢。

<?php

    require_once('connect.php');

    $group = 1;//$_GET['group'];

    $export = mysql_query ("SELECT * FROM relationship WHERE group_id = $group");

    $fields = mysql_num_fields ( $export );

    for ( $i = 0; $i < $fields; $i++ )
    {
        $header .= mysql_field_name( $export , $i ) . "\t";
    }

    while( $row = mysql_fetch_row( $export ) )
    {
        $line = '';
        foreach( $row as $value )
        {                                            
            if ( ( !isset( $value ) ) || ( $value == "" ) )
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\n";
    }
    $data = str_replace( "\r" , "" , $data );

    if ( $data == "" )
    {
        $data = "\n(0) Records Found!\n";                        
    }

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=your_desired_name.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$data";

    mysql_close($my_connection);

    ?>
4

0 回答 0