dpylt7626401 2014-08-04 11:53
浏览 27

我想在没有提示屏幕的情况下将文件保存在服务器目录

A spreadsheet is generated when I submit a form. A prompt is appear which ask me to save file, but I want that when I submit form that spreadsheet automatically saved in my server directory e.g. public_html/apm/test/file.xls.

How can I handle this.

Previously I am using this code

    /* Please acknowledge use of this code by including this header. */

    $data = array(
        array("firstname" => $firstName, "lastname" => $lastName, "age" => $phoneNo, "email" => $email, "dob" => $dob, "mailingAddress" => $mailingAddress, "prescribedPhysician" => $prescribedPhysician, "patientID" => $patientID, "caregiver" => $caregiver, "nameOfCaregiver" => $nameOfCaregiver, "growLocation" => $growLocation, "expirationDate" => $expirationDate, "registrationNo" => $registrationNo, "medicalCondition" => $medicalCondition, "describeMedicalCondition" => $describeMedicalCondition, "grower" => $grower, "tradingCannabis" => $tradingCannabis, "medicalCannabis" => $medicalCannabis, "needCaregiver" => $needCaregiver)
    );

    function cleanData(&$str){
        $str = preg_replace("/\t/", "\\t", $str);
        $str = preg_replace("/?
/", "\
", $str);
        if(strstr($str, '"'))
            $str = '"' . str_replace('"', '""', $str) . '"';
    }

    // file name for download

    $filename = $firstName." ".$lastName." ".date('d-m-y') . ".xls";

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");

    $flag = false;
    foreach($data as $row) {
        if(!$flag) {
            // display field/column names as first row
            echo implode("\t", array_keys($row)) . "
";
            $flag = true;
        }

        array_walk($row, 'cleanData');
        echo implode("\t", array_values($row)) . "
";
    }

    exit;
  • 写回答

1条回答 默认 最新

  • dounai1986 2014-08-04 12:18
    关注

    Your code is doing exactly what you tell it to do: "make this content downloadable for the web client.". This is done by both header directives:

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");
    

    Then, at the end of the code, you're echoing some content. So this content is served to the user

    If you want to save the file on the server, you can't echo the file content, but you have to save this content on the server. You can do it using file_put_contents PHP function.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程