Solved

Formatting print_r($_FILES)

Posted on 2008-10-13
6
1,428 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
The viewer will learn how to count occurrences of each item in an array.

770 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