我正在尝试将数据从帖子保存到我的数组会话变量中。但是不是添加新元素而是覆盖旧元素......我做错了什么?
//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!
}
?>
任何想法?
答案 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']);