簡體   English   中英

包含會話時,我不能使用$ _FILES

[英]When including session I can not use $_FILES

我正在上傳一個csv文件,並將其發送到頁面以使用js fetch api進行處理。 我已經使用自己的init文件包含了會話,並且所有工作都很好地接受了應該處理以歸檔信息的頁面。 如果不包括會話,它可以正常工作,並且我可以處理文件,當包含文件時,我可以看到所有會話信息,但是$ _FILES只是停止工作,由於某種原因,我看不到任何信息。

我真的希望這是愚蠢的

如果需要一些代碼

初始化文件

<?php

    //define Site Root and Includes Path
    defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
    defined('SITE_ROOT') ? null : define('SITE_ROOT', __DIR__ . DS );
    defined('INCLUDES_PATH') ? null : define('INCLUDES_PATH', SITE_ROOT);

    //get class files
    include(INCLUDES_PATH.DS."Session.php");
    require_once(INCLUDES_PATH.DS."functions.php");
    require_once(INCLUDES_PATH.DS."DB.php");
    require_once(INCLUDES_PATH.DS."Validation.php");
    require_once(INCLUDES_PATH.DS."User.php");
    require_once(INCLUDES_PATH.DS."Swp.php");
    require_once(INCLUDES_PATH.DS."Incident.php");
    require_once(INCLUDES_PATH.DS."Hira.php");
    require_once(INCLUDES_PATH.DS."Mail.php");

Session.php頁面

<?php

    class Session
    {
        private $logged_in;
        public $user_id;

        function __construct()
        {
            session_start();
            $this->check_login();
        }

        public function is_logged_in() {

            return $this->logged_in;
        }

        private function check_login() {

            if (isset($_SESSION['UserID'])) {
                $this->user_id = $_SESSION['UserID'];
                $this->logged_in = true;
            } else {
                unset($this->user_id);
                $this->logged_in = false;
            }
        }

    }

    $session = new Session();

表單頁面

<?php
    //get all the includes
    require_once("../php_functions/_init.php");
  print_r($_FILES);
    //all rows from csv file
    $rows = array_map('str_getcsv', file($_FILES['file']['tmp_name'][0]));

    //get only the headers
    $header = array_shift($rows);

    //declare the array
    $csv = array();

    //create associative array using array_combine
    foreach ($rows as $row) {
        $csv[] = array_combine($header, $row);
    }

    print_r($csv);

就像我提到的,如果我從此頁面刪除了一次require,它將按預期工作。 任何想法都會有所幫助

萬一您需要它,這里是用於上傳文件的js

document.querySelector("#upload_file").addEventListener("click", function () {

        const url = 'php/employee/upload_employee_csv_form.php';

        const files = document.querySelector('[type=file]').files;
        const formData = new FormData();

        for (let i = 0; i < files.length; i++) {
            let file = files[i];

            formData.append('file[]', file);
        }

        console.log(formData);

        fetch(url, {
            method: 'POST',
            body: formData
        }).then(response => {
            console.log(response);
        });
    });

我真的想通了。 因此,在我實際添加會話之前將其包含在內,以解決此問題,而不僅僅是說session_start,而是檢查會話是否存在,然后僅在必要時啟動。

編碼

if (session_status() === PHP_SESSION_NONE) {
                session_start();
            }

獎金提示:-)

上面的代碼適用於php 5.4及更高版本,如果您運行的版本低於5.4,則可以執行以下操作:

if(session_id() == '') {
    session_start();
}

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM