PHP file download

has a set of data, write it to txt, and then download the txt file

how should this be implemented

Php
Apr.18,2022

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);

? >
this is an example on w3school
http://www.w3school.com.cn/ph...

< hr >

post the code that downloaded the csv file before. The principle is the same

.
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        $fileName = 'WinterStudentCount-'.date('YmdHis', time());
        header('Content-Type: application/vnd.ms-execl');
        header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
        header('Cache-Control: max-age=0');
        //php
        //
        $fp = fopen('php://output', 'a');
        // 
        $info = ['','','','','',''];
        foreach ($info as $k => $v) {
            $title[] = iconv('UTF-8', 'gbk//IGNORE', $v);
        }
        //
        $data = WinterStudentDataForm::find()->where($map);
        $datainfo = $data->select(['school','year','number','enroll_number'])->asarray()->all();
        for ($i=0; $i < count($datainfo); $iPP) { 
            $datainfo[$i]['rate'] = $datainfo[$i]['enroll_number'].' \ '.$datainfo[$i]['number'];
        }
        $year = isset($map['year'])?$map['year']:'0';
        $number_sum = $data->sum('number');
        $enroll_number_sum = $data->sum('enroll_number');
        $info_count = ['',iconv('UTF-8', 'gbk//IGNORE', ''),$year,$number_sum,$enroll_number_sum,$enroll_number_sum.' \ '.$number_sum];
        if($datainfo) {
            //
            $i = 1;
            fputcsv($fp, $title);
            foreach($datainfo as $key=> $item) {
                //
                $order = $iPP;
                foreach($item as $k => $v){
                    $row['order'] = $order;
                    $row[$k] = iconv('UTF-8', 'gbk//IGNORE', $v);
                }
                fputcsv($fp, $row);
            }
            fputcsv($fp, $info_count);
        }


think of it this way: write the array to txt first, because the array cannot be written directly, and the data can be written after processing, such as json. The second part calls the download API to download this file to the local


php reads csv file data and outputs it, changes CSV into corresponding TXT, and outputs

.
$file = fopen("log.txt", "r");
        $user=array();
        $i=0;
//
        while(! feof($file))
        {
            $user[$i]= fgets($file);//fgets()
            $iPP;
        }
        fclose($file);
        $user=array_filter($user);
        print_r($user);

download



class UserExcelController extends Controller
{
    /**
     * 
     * @param $dataArr  ;
     * @param $fieldArr $dataArrkeyvalue ;
     * @param $tname excel;
     * @author qingfeng.guo
     * 2016-06-21
     */
    function exportExcel($dataArr,$fieldArr,$tname="")
    {
        if(!is_array($fieldArr) || empty($fieldArr))
            die("");

        if(!empty($dataArr) && count($dataArr) > 10000)
            die("10000");

        $thead = ''; //
        $tbody = ''; //
        $ttail = ''; //
        $clos = count($fieldArr); //
        //
        $thead .= '<tr>';
        foreach($fieldArr as $k => $v)
        {
            $thead .= '<td style="color:-sharpfff;background-color:green;">'.$v.'</td>';
        }
        $thead .= '</tr>';
        //
        $ttail = '<tr><td colspan="'.$clos.'"> :'.$tname.' , :'.date("Y-m-d H:i:s").' </td></tr>';

        //
        $datas = array();
        foreach($dataArr as $k => $v)
        {
            $v['created_at'] = date('Y-m-d H:i:s',$v['created_at']);
            $fieldFormat = $fieldArr; //
            if(!empty($v) && is_array($v))
            {
                foreach($v as $k2 => $v2)
                {
        //key
                    if(array_key_exists($k2, $fieldArr))
                    {
                        $fieldFormat[$k2] = $v2;
                    }
                }
            }
            $datas[] = $fieldFormat;
        }
        //
        foreach ($datas as $k => $v)
        {
            $tbody .= '<tr>';
            foreach($v as $k2 => $v2)
            {
                if(!is_numeric($v2))
                {
                    $tbody .= '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
                }else{
                    $tbody .= '<td>'.$v2.'</td>';
                }
            }
            $tbody .= '</tr>';
        }

        $content = $thead.$tbody.$ttail;
        header("content-type:application/vnd.ms-excel; charset=utf8");
        header("Content-Disposition:attachment;filename={$tname}".time().".xls");
        echo "<table align='center' border=1 bgcolor='F8F7ED'>";
        if(mb_detect_encoding($content) != 'UTF-8')
        {
            echo iconv('UTF-8',"GB2312",$content);
        }else{
            echo $content;
        }
        echo "</table>";
    }



    //
    public function userExcel(Request $request){
        $list = User::join('users as u','u.id','=','users.pid')
            ->select('users.id','users.nickname','u.nickname as u_nickname','users.phone','u.phone as u_phone','users.email','users.created_at')
            ->paginate(10)->toArray();
        $list =$list['data'];
        $fieldArr = array(
            'id' => 'id',
            'nickname' => '',
            'u_nickname' => '',
            'phone' =>'',
            'u_phone'=>'',
            'email'=>'',
            'created_at'=>'',
        );
        $this->exportExcel($list, $fieldArr,'');
    }
}
Menu