Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

accessing array data inside of an array

 
   Database Forums (Home) -> PHP RSS
Next:  some thoughts on OOW 2007  
Author Message
bill

External


Since: Nov 24, 2006
Posts: 17



(Msg. 1) Posted: Sun Nov 18, 2007 10:13 am
Post subject: accessing array data inside of an array
Archived from groups: comp>lang>php (more info?)

I want to extract the name of the photo uploaded.

print_r of $_FILES yields:
Files: Array ( [picture] => Array ( [name] =>
DSC_1802-saoirse-riona.jpg [type] => image/jpeg [tmp_name] =>
/tmp/phpJTspHZ [error] => 0 [size] => 76096 ) )

so we have an array with one element, picture, that contains an
array with an index, name, with a value of DSC_1802-saoirse-riona.jpg

I get the correct value when I:
$pic_name = $_FILES['picture']['name'];
echo $pic_name

however when I use:

echo "<br />pic_name = $_FILES[picture][name]";

I get:
pic_name = Array[name]

Why does the echo with the array reference not work while the
assignment does ?

bill

 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
ZeldorBlat

External


Since: Apr 17, 2007
Posts: 44



(Msg. 2) Posted: Sun Nov 18, 2007 10:14 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 18, 10:13 am, bill wrote:
> I want to extract the name of the photo uploaded.
>
> print_r of $_FILES yields:
> Files: Array ( [picture] => Array ( [name] =>
> DSC_1802-saoirse-riona.jpg [type] => image/jpeg [tmp_name] =>
> /tmp/phpJTspHZ [error] => 0 [size] => 76096 ) )
>
> so we have an array with one element, picture, that contains an
> array with an index, name, with a value of DSC_1802-saoirse-riona.jpg
>
> I get the correct value when I:
> $pic_name = $_FILES['picture']['name'];
> echo $pic_name
>
> however when I use:
>
> echo "<br />pic_name = $_FILES[picture][name]";
>
> I get:
> pic_name = Array[name]
>
> Why does the echo with the array reference not work while the
> assignment does ?
>
> bill

Put quotes around your array keys:

$_FILES['picture']['name']

 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
bill

External


Since: Nov 24, 2006
Posts: 17



(Msg. 3) Posted: Sun Nov 18, 2007 10:47 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

ZeldorBlat wrote:
> On Nov 18, 10:13 am, bill wrote:
>> I want to extract the name of the photo uploaded.
>>
>> print_r of $_FILES yields:
>> Files: Array ( [picture] => Array ( [name] =>
>> DSC_1802-saoirse-riona.jpg [type] => image/jpeg [tmp_name] =>
>> /tmp/phpJTspHZ [error] => 0 [size] => 76096 ) )
>>
>> so we have an array with one element, picture, that contains an
>> array with an index, name, with a value of DSC_1802-saoirse-riona.jpg
>>
>> I get the correct value when I:
>> $pic_name = $_FILES['picture']['name'];
>> echo $pic_name
>>
>> however when I use:
>>
>> echo "<br />pic_name = $_FILES[picture][name]";
>>
>> I get:
>> pic_name = Array[name]
>>
>> Why does the echo with the array reference not work while the
>> assignment does ?
>>
>> bill
>
> Put quotes around your array keys:
>
> $_FILES['picture']['name']

That, unfortunately, generates the dreaded:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING in...

bill
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Rik Wasmus

External


Since: Sep 02, 2007
Posts: 210



(Msg. 4) Posted: Sun Nov 18, 2007 4:00 pm
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 18 Nov 2007 16:47:17 +0100, bill wrote:

> ZeldorBlat wrote:
>> On Nov 18, 10:13 am, bill wrote:
>>> I want to extract the name of the photo uploaded.
>>>
>>> print_r of $_FILES yields:
>>> Files: Array ( [picture] => Array ( [name] =>
>>> DSC_1802-saoirse-riona.jpg [type] => image/jpeg [tmp_name] =>
>>> /tmp/phpJTspHZ [error] => 0 [size] => 76096 ) )
>>>
>>> so we have an array with one element, picture, that contains an
>>> array with an index, name, with a value of DSC_1802-saoirse-riona.jpg
>>>
>>> I get the correct value when I:
>>> $pic_name = $_FILES['picture']['name'];
>>> echo $pic_name
>>>
>>> however when I use:
>>>
>>> echo "<br />pic_name = $_FILES[picture][name]";
>>>
>>> I get:
>>> pic_name = Array[name]
>>>
>>> Why does the echo with the array reference not work while the
>>> assignment does ?
>>>
>>> bill
>> Put quotes around your array keys:
>> $_FILES['picture']['name']
>
> That, unfortunately, generates the dreaded:
> Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting T_STRING or T_VARIABLE or T_NUM_STRING in...

Look at complex (curly) syntax:
<http://nl2.php.net/manual/en/language.types.string.php#language.types.string.parsing>

echo "<br />pic_name = {$_FILES['picture']['name']}";


--
Rik Wasmus
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Toby A Inkster

External


Since: Nov 13, 2007
Posts: 29



(Msg. 5) Posted: Sun Nov 18, 2007 4:00 pm
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

bill wrote:

> echo "<br />pic_name = $_FILES[picture][name]";

echo "<br />pic_name = {$_FILES[picture][name]}";

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 12 days, 2 min.]
[Now Playing: ./orson/bright_idea/03_-_happiness.ogg.]

USD/EUR Exchange Rate Graph
http://tobyinkster.co.uk/blog/2007/11/18/usd-eur/
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Rik Wasmus

External


Since: Sep 02, 2007
Posts: 210



(Msg. 6) Posted: Mon Nov 19, 2007 1:59 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 18 Nov 2007 18:05:32 +0100, Toby A Inkster
wrote:

> bill wrote:
>
>> echo "<br />pic_name = $_FILES[picture][name]";
>
> echo "<br />pic_name = {$_FILES[picture][name]}";
>

define('picture','oops');
define('name','wrong');
--
Rik Wasmus
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Toby A Inkster

External


Since: Nov 13, 2007
Posts: 29



(Msg. 7) Posted: Mon Nov 19, 2007 6:00 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Rik Wasmus wrote:

> define('picture','oops');
> define('name','wrong');

I know, I know, but I wanted to keep the code as similar as possible to
the OP's.

Besides which, it's not normal to define lower-case constants.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 12 days, 15:31.]

USD/EUR Exchange Rate Graph
http://tobyinkster.co.uk/blog/2007/11/18/usd-eur/
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
bill

External


Since: Nov 24, 2006
Posts: 17



(Msg. 8) Posted: Mon Nov 19, 2007 7:23 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Toby A Inkster wrote:
> bill wrote:
>
>> echo "<br />pic_name = $_FILES[picture][name]";
>
> echo "<br />pic_name = {$_FILES[picture][name]}";
>
Rik and Toby
Thanks, something new to learn.

bill
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Toby A Inkster

External


Since: Nov 13, 2007
Posts: 29



(Msg. 9) Posted: Mon Nov 19, 2007 7:59 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Toby A Inkster wrote:

> Besides which, it's not normal to define lower-case constants.

And people who do so are not normal. They have something wrong with them
in their head. Just thinking about the idea makes me feel icky.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 12 days, 17:44.]

USD/EUR Exchange Rate Graph
http://tobyinkster.co.uk/blog/2007/11/18/usd-eur/
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Rik Wasmus

External


Since: Sep 02, 2007
Posts: 210



(Msg. 10) Posted: Mon Nov 19, 2007 1:59 pm
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 19 Nov 2007 09:34:23 +0100, Toby A Inkster
wrote:
> Rik Wasmus wrote:
>> define('picture','oops');
>> define('name','wrong');
>
> I know, I know, but I wanted to keep the code as similar as possible to
> the OP's.

Then again, why correct just one mistake and not the other?

> Besides which, it's not normal to define lower-case constants.

Very true indeed.
--
Rik Wasmus
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Rik Wasmus

External


Since: Sep 02, 2007
Posts: 210



(Msg. 11) Posted: Mon Nov 19, 2007 1:59 pm
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 19 Nov 2007 13:23:08 +0100, bill wrote:

> Toby A Inkster wrote:
>> bill wrote:
>>
>>> echo "<br />pic_name = $_FILES[picture][name]";
>> echo "<br />pic_name = {$_FILES[picture][name]}";
>>
> Rik and Toby
> Thanks, something new to learn.

Hmm, now I think about it: offcourse you never ever change the $_FILES
array yourself, you just use it's information. There is no way you should
echo user supplied strings directly to the page. At least use
htmlentities() on them before displaying them, or you're vulnerable to so
called XSS attack.

See <http://en.wikipedia.org/wiki/Cross_site_scripting>, Type 1 (and
possibly 2 depending on further processing).
--
Rik Wasmus
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
bill

External


Since: Nov 24, 2006
Posts: 17



(Msg. 12) Posted: Tue Nov 20, 2007 7:19 am
Post subject: Re: accessing array data inside of an array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Rik Wasmus wrote:
> On Mon, 19 Nov 2007 13:23:08 +0100, bill wrote:
>
>> Toby A Inkster wrote:
>>> bill wrote:
>>>
>>>> echo "<br />pic_name = $_FILES[picture][name]";
>>> echo "<br />pic_name = {$_FILES[picture][name]}";
>>>
>> Rik and Toby
>> Thanks, something new to learn.
>
> Hmm, now I think about it: offcourse you never ever change the $_FILES
> array yourself, you just use it's information. There is no way you
> should echo user supplied strings directly to the page. At least use
> htmlentities() on them before displaying them, or you're vulnerable to
> so called XSS attack.

just for debugging. Not a live application
>
> See <http://en.wikipedia.org/wiki/Cross_site_scripting>, Type 1 (and
> possibly 2 depending on further processing).

thanks for the reminder however

bill
 >> Stay informed about: accessing array data inside of an array 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Incoming data into array? - Hi, I'm opening a remote file on a regular basis, the data is in the following format and has a line feed of "0D 0A". The spaces are 20 in hex mode. 2006-07-26 22:00:03.000000 4.370996 51.656616 2006-07-26 22:00:03.000000 2.599964 49.286836 ...

Form Data -> Variables or an Array? - Hi I need help! Forgive me I am a PHP newbie. I have a small script that enables me to send a form from an HTML page. I want to use the HTML formatted form because the design of my website is complex, and I don't want to have to mess around with..

Array - Hi Can you help me sort my array on salary $Person[$personid][salary] e.g. $Person[1][20000] $Person[2][5000] $Person[3][15000] $Person[4][10000] to $Person[2][5000] $Person[4][10000] $Person[3][15000] $Person[1][20000] Thanx

SUM of array - I want to get the sum of the array key values. ex: $key1=>10, $key2=>15, $key3=>5 I want variable $sum = 30. any solution?

array() VS Array() - which one is recommend? seems they perform the same thing... regards, howa
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]