PHP SESSION ARRAY由post变量覆盖

时间:2013-10-28 02:58:06

标签: php arrays session

我正在尝试将数据从帖子保存到我的数组会话变量中。但是不是添加新元素而是覆盖旧元素......我做错了什么?

//PHP CODE !!!
$_SESSION['file[]'] = $_FILES['file'];
$_SESSION['file_names[]'] = $_POST['file_name'];
print_r($_SESSION['file[]']); //it will only display last selected file. :(

//HTML !!!
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
   <input type="input" name="file_name" value="">
   <input type="file" name="file" id="file" value="">
   <input type="submit" name="SEND" value="ADD"/>
</form>

//LITLE CHECK CODE HERE !!!
//THIS CODE WILL DISPLAY " OUT OF IF STATEMENT !!!" ;/

<?php
    if(isset($_SESSION['file_names[]']) && is_array($_SESSION['file_names[]'])){
        foreach($_SESSION['file_names[]'] as $index => $name){
                     /...
        }
    }else{
        echo " OUT OF IF STATEMENT !!!"; //executed!
    }
?>

任何想法?

1 个答案:

答案 0 :(得分:4)

我想你想制作$_SESSION多维数组,所以改变:

$_SESSION['file[]'] = $_FILES['file'];
$_SESSION['file_names[]'] = $_POST['file_name'];

$_SESSION['file'][] = $_FILES['file']; //assign $_FILES data to session array
$_SESSION['file_names'][] = $_POST['file_name']; //assign $_POST data to session array
print_r($_SESSION['file']);