Eman_321 Posted August 5, 2010 Share Posted August 5, 2010 Hi, I'm a PHP beginner using Julie Meloni Sams Teach yourself to help me. My confusion is with this script <?php $file_dir ="/path/to/upload/directory" ; foreach($_FILES as $file_name => $file_array) { echo "path: ".$file_array["tmp_name"]."<br/>\n" ; echo "name: ".$file_array["name"]."<br/>\n" ; echo "type: ".$file_array["type"]."<br/>\n" ; echo "File name is" . " ". $file_name ; } ?> What I don't understand is the foreach statement foreach($_FILES as $file_name => $file_array) $_FILES is an associative array, right? So $filename should represent the key id and $file_array the value. but she is using the $file_array as if it is an array and accessing the values with associative index which confuses. I mean, let's say I declared a normal array. $array = array("Sarah" => "Jane", "John Smith") ; if i wanted to use a foreach loop i would do it like so foreach($array as $fname => $surname) { echo $fname ; echo $surname ; } $fname will print the first name and surname surname. I didn't use an indices to access the values. foreach($_FILES as $file_name => $file_array) can someone explain this to me? Thanks Quote Link to comment Share on other sites More sharing options...
phil88 Posted August 5, 2010 Share Posted August 5, 2010 You can have arrays of arrays. I can't remember the exact set up of $_FILES, but I suspect it's something like; $_FILES = array( 'filename.png' =>array( 'tmp_name' => 'sadfasdf.tmp', 'name' => 'filename.png', 'type' => 'image/png' ), 'filename2.jpg' =>array( 'tmp_name' => 'sadfasasdfwdf.tmp', 'name' => 'filename2.jpg', 'type' => 'image/jpeg' ) ); So $_FILE's elements can be accessed by string keys, but each element in $_FILE can also be accessed by their string keys because they're also associative arrays. Quote Link to comment Share on other sites More sharing options...
Eman_321 Posted August 5, 2010 Author Share Posted August 5, 2010 Oh my God thanks very much man your explanation is spot on. I posted this question in 3 forums, and no one replied. So I could have many file upload options like so <input type="file" name="fileupload"/> <input type="file" name="fileupload"/> let's say about four of them. And I should imagine that if i do it right i can upload all of them with one foreach loop. Since the $_FILES can contain multiple arrays (i'm not so sure because they use the same name "fileupload"). I am going to try it now. I think I get it now thanks Quote Link to comment Share on other sites More sharing options...
Eman_321 Posted August 5, 2010 Author Share Posted August 5, 2010 yep it works. All i had to do was change file name. Thanks Quote Link to comment Share on other sites More sharing options...
Eman_321 Posted August 5, 2010 Author Share Posted August 5, 2010 I'm having a new problem When i upload a file, and i print out the the name, temp directory, and size. It prints the name alright. But it doesn't print out the temp path or the type. It works for file types like gif, png or music files. but not for tga or pdf files. I'm not sure why that's happening. Maybe you guys can have a look at the source code i uploaded [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.