dongshao5573
2018-10-09 12:26
浏览 55
已采纳

包含会话时我不能使用$ _FILES

I am uploading a csv file and sending it to page to process using the js fetch api. I have session included using my init file and all works well accept for the page that should process to file info. Without including the session it works fine and I can process the file, when including it, I can see al the session info, but $_FILES just stops working, I can't see any of the info for some reason.

I really hope this is something stupid

Some code if needed

The init file

<?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");

The Session.php page

<?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();

The form page

<?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);

like I mentioned if I removed the require once from this page it works as expected. Any ideas would help

Just in case you need it here is the js for uploading the file

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);
        });
    });
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhuai2015 2018-10-10 12:56
    已采纳

    I actually figured it out. So my session was being included before I actually added it, to fix this instead of just saying session_start I check to see if the session is there and then only start if necessary.

    The code

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

    Bonus tip :-)

    The above code will work for php 5.4 and up if you are running something lower than 5.4 you can do the following:

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

    Hope this helps

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题