473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Traversing through $_FILES array

Markus
6,050 Expert 4TB
Ok, so i'm doing the multiple file upload thingymabob!

I'm just playing around really...

When i try to check the file type i simply get Array().

[php]
foreach ($_FILES['file']['name'] as $_key => $_value)
{
echo $_value . " was uploaded successfully.<br />";
echo $_FILES['file']['type'] . " <br />";
}
[/php]

So to traverse through the array i added a foreach:

[php]
foreach ($_FILES['file']['name'] as $_key => $_value)
{
echo $_value . " was uploaded successfully.<br />";
foreach($_FILES['file']['type'] as $_x => $_y){
echo $_value . " is in $_y format<br />";
}
}
[/php]
But that gives me this:
Expand|Select|Wrap|Line Numbers
  1. ut_the_folk_down1.gif was uploaded successfully.
  2. put_the_folk_down1.gif is in image/gif format
  3. put_the_folk_down1.gif is in image/jpeg format
  4. natnme.jpg was uploaded successfully.
  5. natnme.jpg is in image/gif format
  6. natnme.jpg is in image/jpeg format
  7.  
When i just want:
Expand|Select|Wrap|Line Numbers
  1. ut_the_folk_down1.gif was uploaded successfully.
  2. put_the_folk_down1.gif is in image/gif format
  3. natnme.jpg was uploaded successfully.
  4. natnme.jpg is in image/jpeg format
  5.  
I'm stumped ...
Jan 21 '08 #1
3 9359
MarkoKlacar
296 Expert 100+
Hi,

I think it's because you loop though the whole array, if you index the array using the filename, $_value, you should only get the filetype for that one.

Now you're getting the whole content.

Hope this helps.

MK
Jan 22 '08 #2
svenni
4
Yup. MarkoKlacar is correct. I think you should write something like this instead:

[PHP]
foreach ($_FILES as $_key => $_value)
{
echo $_FILES[$_key]['name'] . " was uploaded successfully.<br />";
echo $_FILES[$_key]['name'] . " is in " . $_FILES[$_key]['type'] . " format<br />";
}
[/PHP]
I'm not quite sure if it will work since I can't test it on this computer. But try it out ;) The syntax might be wrong, but I guess the structure is correct.
Jan 22 '08 #3
Markus
6,050 Expert 4TB
Thanks guys!

After stirring the logic soup in my brain a little, you guided me into this:

[php]
foreach ($_FILES['file']['name'] as $_key => $_value)
{
echo $_value . " was uploaded successfully.<br />";
echo $_FILES['file']['type'][$_key]."<br />";
}
[/php]

I originally tried to put the key before the ['type'] thinking that the key would have to come immediately after the file array, but i was wrong :P

Thanks again!
Jan 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mrbog | last post by:
All I did was recompile my PHP 4.3.4 with the included GD and now my $_FILES is perpetually empty. My php app is identical, I haven't changed it at all since recompiling. I tried recompiling...
4
by: Ian.H | last post by:
Hi all, Uploading of files.. AFAIU from the manual, if a file is larger than the size defined in the form or larger than upload_max_filesize in php.ini, that _FILES should hold an INT error...
1
by: Bob Bedford | last post by:
I've a function asking for a $_FILES argument: function insertarticle($particle,......,$pUserID,$pOptions,$files){ in this function I do add the record and then save the files in a subdirectory...
6
by: Shelly | last post by:
I have searched and have not gotten an understanding of $_FILES. I am trying to do an upload to a directory "tmpdir" on the server. I have a field defined as: File name: <input...
4
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the...
2
by: chris | last post by:
Can someone else see immediately why this script: <html> <body> <?php error_reporting(~E_ALL); print_r($_FILES); print_r($_POST);
8
by: mpar612 | last post by:
Hello, I am a newbie to PHP, MySQL. I am trying to create a basic file upload form. I want to get that working and then I want to integrate that into a form that will rename the file and save...
1
by: wbsurfver | last post by:
I'm trying to figure out why I can't upload a file in php. I'm trying it locally on a windows XP machine running Apache 2.2 and PHP 5.2.1. That is I am running the browser/server on same machine...
3
by: skulkrinbait | last post by:
I've a HTML form that allows a user to specify the location to upload a file from: <p><label for = 'file'>Upload Graphics : <input type='file' name='imagefile' /></label></p> I then want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.