Solved

Formatting print_r($_FILES)

Posted on 2008-10-13
6
1,451 Views
Last Modified: 2012-05-05
Hi guys,
Im working on php upload stuff at the moment, and was wondering if there is a nice way of displaying in a browser all the key value pairs for a $_FILES array.
At the moment, im just doing the following..

print_r($_FILES);

which gives me a bit of a messy output..

Array ( [userfile] => Array ( [name] => Array ( [0] => tooltipdb_0000003.jpg [1] => [2] => [3] => ) [type] => Array ( [0] => image/pjpeg [1] => [2] => [3] => ) [tmp_name] => Array ( [0] => c:/wamp/tmp\php14B2.tmp [1] => [2] => [3] => ) [error] => Array ( [0] => 0 [1] => 4 [2] => 4 [3] => 4 ) [size] => Array ( [0] => 333635 [1] => 0 [2] => 0 [3] => 0 ) ) )

Any help on formatting this output much appreciated.
0
Comment
Question by:Simon336697
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Assisted Solution

by:hampus_b
hampus_b earned 220 total points
ID: 22701948
add <pre> before and </pre> after and it will be readable.

But to take control over the output; do like this:


<?
 
foreach ($array AS $key => $value) {
 echo "<strong>$key</strong>";
 echo "<br />";
echo $value;
echo "<br />"
}
 
?>

Open in new window

0
 
LVL 6

Accepted Solution

by:
Chorch earned 280 total points
ID: 22701953
hello,

try this:

<?php
function echo_array($array,$return_me){
    if(is_array($array) == false){
        $return = "The provided variable is not an array.";
    }else{
        foreach($array as $name=>$value){
            if(is_array($value)){
                $return .= "";
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
                $return .= echo_array($value,true);
                $return .= "</div>}";
                $return .= "\n\n";
            }else{
                if(is_string($value)){
                    $value = "\"$value\"";
                }
                $return .= "['<b>$name</b>'] = $value\n\n";
            }
        }
    }
    if($return_me == true){
        return $return;
    }else{
        echo "<pre>".$return."</pre>";
    }
}

echo_array($_FILES, 'false');
?>

Regards
0
 
LVL 6

Expert Comment

by:hampus_b
ID: 22701956
i mean like this:
echo "<pre>";
print_r($_FILES);
echo "</pre>";

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Simon336697
ID: 22705316
Hi Chorch,
Thanks so much for spending the time on this.
I ran this but it does not return any output at all.
That is, it doesnt return if it is not an array eg.
$return = "The provided variable is not an array.";
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22705546
My mistake...you guys are the best...all is good Chorch...it works..
Thanks Chorch very much and hampus_b:
0
 
LVL 6

Expert Comment

by:Chorch
ID: 22706902
Thanks, glad to see I helped you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question