0

I've been using PHP for a little bit, but only doing basic tasks, and have gotten stuck while trying to build part of a knowledgebase.

While creating an article, you can upload screenshots - these need to be stored in a staging area until the article is saved, and I've got their information saved into the users session.

In POST upload:

...
$new_ss = array();
$new_ss['display_name'] = $ss_name;
$new_ss['server_path'] = "../uploads/";    /* outside of doc root */
$new_ss['server_path'] .= hash_file('md5', $ss_tmp_name) . "_" . $ss_name;

if ( @move_uploaded_file($ss_tmp_name, $new_ss['server_path']) )
{
    $_SESSION['article']['screenshots'] .= $new_ss;
    ...

Then trying to display the upload to the user within the editing page:

if ( is_array($_SESSION['article']['screenshots']) && count($_SESSION['article']['screenshots']) > 0 )
{
    echo "<table border=\"1\">\n";
    foreach ( $_SESSION['article']['screenshots'] as $ss )
    {
        ... display table of uploaded images ...

The trouble is, PHP isn't seeing $_SESSION['article']['screenshots'] as an array - and I can't see why.

After following the answer here: PHP foreach() with arrays within arrays? I tried quickly doing the following:

function print_array($key, $item) {
    echo "$key -> $item\n";
}

...
{
    $_SESSION['article']['screenshots'] .= $new_ss;
    array_walk_recurisve($_SESSION['article']['screenshots'], 'print_array');
    ...

however I get the error "expects parameter 1 to be array, string given".

If I do an 'error_log($_SESSION['article']['screenshots']);' it just prints "Array" for however many images are uploaded, and print_r just printed '1' by itself.

Can someone shed some light please?

1 Answer 1

1

Well I'm not really sure which of your key should be an array, in this line:

$_SESSION['article']['screenshots'] .= $new_ss;

You're making it a string by concatenating it. If you want to append as an array, you should probably do this:

 $_SESSION['article']['screenshots'][] = $new_ss;
Sign up to request clarification or add additional context in comments.

1 Comment

Argh, so simple! Was stuck in the mindset of 'appending to', and that operator was it... working now thanks! Will accept once I'm allowed to!

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.