accessing array data inside of an array

accessing array data inside of an array

am 18.11.2007 16:13:59 von NerdRevenge

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 "
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

Re: accessing array data inside of an array

am 18.11.2007 16:31:21 von zeldorblat

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 "
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']

Re: accessing array data inside of an array

am 18.11.2007 16:47:17 von NerdRevenge

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 "
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

Re: accessing array data inside of an array

am 18.11.2007 17:42:43 von luiheidsgoeroe

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] =3D> Array ( [name] =3D>
>>> DSC_1802-saoirse-riona.jpg [type] =3D> image/jpeg [tmp_name] =3D>
>>> /tmp/phpJTspHZ [error] =3D> 0 [size] =3D> 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.jp=
g
>>>
>>> I get the correct value when I:
>>> $pic_name =3D $_FILES['picture']['name'];
>>> echo $pic_name
>>>
>>> however when I use:
>>>
>>> echo "
pic_name =3D $_FILES[picture][name]";
>>>
>>> I get:
>>> pic_name =3D 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:
tring.parsing>

echo "
pic_name =3D {$_FILES['picture']['name']}";


-- =

Rik Wasmus

Re: accessing array data inside of an array

am 18.11.2007 18:05:32 von Toby A Inkster

bill wrote:

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

echo "
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/

Re: accessing array data inside of an array

am 19.11.2007 04:47:35 von luiheidsgoeroe

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

wrote:

> bill wrote:
>
>> echo "
pic_name =3D $_FILES[picture][name]";
>
> echo "
pic_name =3D {$_FILES[picture][name]}";
>

define('picture','oops');
define('name','wrong');
-- =

Rik Wasmus

Re: accessing array data inside of an array

am 19.11.2007 09:34:23 von Toby A Inkster

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/

Re: accessing array data inside of an array

am 19.11.2007 11:48:07 von Toby A Inkster

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/

Re: accessing array data inside of an array

am 19.11.2007 13:23:08 von NerdRevenge

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

bill

Re: accessing array data inside of an array

am 19.11.2007 18:02:03 von luiheidsgoeroe

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

Re: accessing array data inside of an array

am 19.11.2007 19:13:30 von luiheidsgoeroe

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

> Toby A Inkster wrote:
>> bill wrote:
>>
>>> echo "
pic_name =3D $_FILES[picture][name]";
>> echo "
pic_name =3D {$_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 shoul=
d =

echo user supplied strings directly to the page. At least use =

htmlentities() on them before displaying them, or you're vulnerable to s=
o =

called XSS attack.

See , Type 1 (and =

possibly 2 depending on further processing).
-- =

Rik Wasmus

Re: accessing array data inside of an array

am 20.11.2007 13:19:10 von NerdRevenge

Rik Wasmus wrote:
> On Mon, 19 Nov 2007 13:23:08 +0100, bill wrote:
>
>> Toby A Inkster wrote:
>>> bill wrote:
>>>
>>>> echo "
pic_name = $_FILES[picture][name]";
>>> echo "
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 , Type 1 (and
> possibly 2 depending on further processing).

thanks for the reminder however

bill