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
Solved

Formatting print_r($_FILES)

Posted on 2008-10-13
6
1,440 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…

808 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