1

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

//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!
    }
?>

有什么想法吗?

4

1 回答 1

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']);
于 2013-10-28T03:00:22.553 回答