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
https://forums.phpfreaks.com/topic/8313-trouble-understanding-arrays/
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.