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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备