Jump to content

Trouble Understanding Arrays...


PWD

Recommended Posts

I've written the following multiple file upload script which works flawlessly:

[code]if(!empty($_POST['go'])) {

foreach($_FILES['fileup']['error'] as $key => $error) {
  if($error == UPLOAD_ERR_OK) {
    $filename = $_FILES['fileup']['name'][$key];
    $filetmp = $_FILES['fileup']['tmp_name'][$key];
    $filetype = $_FILES['fileup']['type'][$key];
    $filesize = $_FILES['fileup']['size'][$key];
    $filedest = 'uploads/';

  if($filetype == 'image/jpeg') {    
    move_uploaded_file($filetmp, $filedest . $filename);
  }
}
}

include 'success.php';

}

else {
  echo print_r($_FILES);
}  [/code]

[b]My Problem:[/b] My 'success.php' page is suppose to list a confirmation for each image uploaded, yet as it stands, it will ONLY display the last image's information. Yet all 3 images upload fine. I assume I'll have to put all the information into an array?!?

Here is 'success.php':
[code]
<p>
    Here is a recap of the photos you uploaded:
</p>

<ul class="none">

    <li>Image Name: <?php echo $filename; ?></li>
    <li>Image Type: <?php echo $filetype; ?></li>
    <li>Image Size: <?php echo $filesize; ?> bytes</li>

</ul>
[/code]

My gratitude ahead of time for helping me learn...
Link to comment
Share on other sites

There are two ways i can think of, one is to put the information into an array as you go, second is to include it in the loop

Please note i havent tested these properly, so there may be syntax errors, but it should give you an idea

[code]if(!empty($_POST['go'])) {

foreach($_FILES['fileup']['error'] as $key => $error) {
  if($error == UPLOAD_ERR_OK) {
    $filename = $_FILES['fileup']['name'][$key];
    $filetmp = $_FILES['fileup']['tmp_name'][$key];
    $filetype = $_FILES['fileup']['type'][$key];
    $filesize = $_FILES['fileup']['size'][$key];
    $filedest = 'uploads/';

  if($filetype == 'image/jpeg') {    
    move_uploaded_file($filetmp, $filedest . $filename);
    
    $theFile[] = $filename;
    $theType[] = $filetype;
    $theSize[] = $filesize;
  }
}
}

include 'success.php';

}

else {
  echo print_r($_FILES);
}  


// Success.php

<p>
    Here is a recap of the photos you uploaded:
</p>
<? $num = count($theFile);

for($i = 0; $i < $num; $i++){ ?>
<ul class="none">

    <li>Image Name: <?php echo $theFile[$i]; ?></li>
    <li>Image Type: <?php echo $theType[$i]; ?></li>
    <li>Image Size: <?php echo $theSize[$i]; ?> bytes</li>

</ul>
<?
} ?>
[/code]

[code]if(!empty($_POST['go'])) {

echo "<p>Here is a recap of the photos you uploaded:</p>";

foreach($_FILES['fileup']['error'] as $key => $error) {
    if($error == UPLOAD_ERR_OK) {
        $filename = $_FILES['fileup']['name'][$key];
        $filetmp = $_FILES['fileup']['tmp_name'][$key];
        $filetype = $_FILES['fileup']['type'][$key];
    $filesize = $_FILES['fileup']['size'][$key];
    $filedest = 'uploads/';

    if($filetype == 'image/jpeg') {
    
        move_uploaded_file($filetmp, $filedest . $filename);
        <ul class="none">

    <li>Image Name: <?php echo $filename; ?></li>
    <li>Image Type: <?php echo $filetype; ?></li>
    <li>Image Size: <?php echo $filesize; ?> bytes</li>

</ul><br><br>
        }
    }
    }

include 'success.php';

}

else {
    echo print_r($_FILES);
}  [/code]
Link to comment
Share on other sites

[b]KENRBNSN:[/b] When I put the include() into the foreach() loop, it gives me 3 pages; one right on top of another...

[b]SANFLY: [/b] Exactly the brain storm I was looking for....

Always grateful to the both of you....
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.