Solved

Formatting print_r($_FILES)

Posted on 2008-10-13
6
1,398 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
Comment Utility
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
Comment Utility
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
Comment Utility
i mean like this:
echo "<pre>";

print_r($_FILES);

echo "</pre>";

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:Simon336697
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, glad to see I helped you!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now