1

I am trying to save data from a post to my array session variable. But instead of adding new element it overwrites the old one... What am I doing wrong ?

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

Any ide ?

3
  • you cannot store arrays in session variables like that way, you have to use seperate session variables to each array element... $_SESSION['file_names[]'] means just one variable named 'file_name[]' Commented Oct 28, 2013 at 3:01
  • sidenote: is that a typo that you close a form tag with an <form>? Commented Oct 28, 2013 at 3:07
  • yes it is typo :> its ok in my source code i will correct this now. Thx Commented Oct 28, 2013 at 3:15

1 Answer 1

4

i think you want to make the $_SESSION multi dimensional array, so change:

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

to

$_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']);
Sign up to request clarification or add additional context in comments.

1 Comment

let me try this brb ! And I am back now :) Love you Sudhir. Works like a charm ! :>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.