Jump to content

PHP File upload script confusion


Eman_321

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D  ;)

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.