Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting print_r($_FILES)

Posted on 2008-10-13
6
Medium Priority
?
1,483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Assisted Solution

by:hampus_b
hampus_b earned 880 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 1120 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

722 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