简体   繁体   English

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

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

I am uploading a csv file and sending it to page to process using the js fetch api. 我正在上传一个csv文件,并将其发送到页面以使用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. 我已经使用自己的init文件包含了会话,并且所有工作都很好地接受了应该处理以归档信息的页面。 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. 如果不包括会话,它可以正常工作,并且我可以处理文件,当包含文件时,我可以看到所有会话信息,但是$ _FILES只是停止工作,由于某种原因,我看不到任何信息。

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

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. 就像我提到的,如果我从此页面删除了一次require,它将按预期工作。 Any ideas would help 任何想法都会有所帮助

Just in case you need it here is the js for uploading the file 万一您需要它,这里是用于上传文件的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);
        });
    });

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. 因此,在我实际添加会话之前将其包含在内,以解决此问题,而不仅仅是说session_start,而是检查会话是否存在,然后仅在必要时启动。

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: 上面的代码适用于php 5.4及更高版本,如果您运行的版本低于5.4,则可以执行以下操作:

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中使用特定于会话的数据文件 - How can I use session specific data files in javascript 我如何在 React 中使用 PHP Session? - How i can use PHP Session in React? 如何在脚本中使用会话? - How can I use session in script? 包含部分内容时可以传递php变量吗? - Can i pass php variables when including a partial? 包括外部js库和js代码文件时使用jquery noconflict函数 - Use of jquery noconflict function when including external js libraries and js code files 当我使用 express.js 时,如何渲染 css 文件? - how can I render the css files when i use express.js? 我是否应该始终使用动态脚本标记注入技术来包含javascript文件,类似于Facebook? - Should I always use dynamic script tag injection technique for including javascript files, similar to Facebook? 如何阻止Require.js优化器在优化文件中包含文本插件? - How can I prevent the Require.js optimizer from including the text plugin in optimized files? 如何在不包含源文件的情况下在 typescript (tsdx) 中编译项目? - How can I compile a project in typescript (tsdx) without including the source files? 如何使用 getServerSideProps 使用 next-iron-session 实现身份验证,而不在每个页面上包含相同的代码 - How can I implement authentication with next-iron-session using getServerSideProps without including the same code on each page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM