Jump to content


Photo

Trouble Understanding Arrays...


  • Please log in to reply
3 replies to this topic

#1 PWD

PWD
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 24 April 2006 - 11:08 PM

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

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);
}  

My Problem: 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':
<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>

My gratitude ahead of time for helping me learn...
[!--fonto:Geneva--][span style="font-family:Geneva"][!--/fonto--]My Gratitude---[!--fontc--][/span][!--/fontc--]
(My gratitude in advance for helping me learn; so I may one day give as freely as you have here today)
----------
[!--sizeo:5--][span style="font-size:18pt;line-height:100%"][!--/sizeo--][!--fonto:Optima--][span style="font-family:Optima"][!--/fonto--][!--coloro:red--][span style="color:red"][!--/coloro--]PWD[!--sizec--][/span][!--/sizec--][!--colorc--][/span][!--/colorc--][!--fontc--][/span][!--/fontc--]

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 11:19 PM

You have the include outside the foreach loop. If you had lined up the brackets better, that would have been made very clear. Good indentation is your friend.

Ken

#3 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 24 April 2006 - 11:24 PM

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

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

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);
}  

If you're not part of the solution, you're part of the precipitate

#4 PWD

PWD
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 24 April 2006 - 11:30 PM

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

SANFLY: Exactly the brain storm I was looking for....

Always grateful to the both of you....
[!--fonto:Geneva--][span style="font-family:Geneva"][!--/fonto--]My Gratitude---[!--fontc--][/span][!--/fontc--]
(My gratitude in advance for helping me learn; so I may one day give as freely as you have here today)
----------
[!--sizeo:5--][span style="font-size:18pt;line-height:100%"][!--/sizeo--][!--fonto:Optima--][span style="font-family:Optima"][!--/fonto--][!--coloro:red--][span style="color:red"][!--/coloro--]PWD[!--sizec--][/span][!--/sizec--][!--colorc--][/span][!--/colorc--][!--fontc--][/span][!--/fontc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users