Click here to Skip to main content
15,896,481 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I've some code for uploading multiple images, I got it from a website.

Here's it :
PHP
<?php

$upload_image_limit = 5; // How many images you want to upload at once?
$upload_dir			= "images_pending/"; // default script location, use relative or absolute path


################################# UPLOAD IMAGES
	
	foreach($_FILES as $k => $v){ 

	$img_type = "";

	### $htmo .= "$k => $v<hr />"; 	### print_r($_FILES);

	if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < 1000000){

	$img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ;
	$img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ;
	$img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ;

	$img_rname = $_FILES[$k]['name'];
	$img_path = $upload_dir.$img_rname;

	copy( $_FILES[$k]['tmp_name'], $img_path ); 
	$feedback .= "Image and thumbnail created $img_rname<br />";

			}
		}






############################### HTML FORM
	while($i++ < $upload_image_limit){
	$form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
	}

	$htmo .= '
	<p>'.$feedback.'</p>
	<form method="post" enctype="multipart/form-data">
	'.$form_img.' <br />
	<input type="submit" value="Upload Images!" style="margin-left: 50px;" />
	</form>
	';	

	echo $htmo;


It works awesome. But could someone explain me briefly about what happens in there? in that foreach loop? I know PHP very little. Nothing advanced. Can help?

Thanks^2 !
Posted
Comments
Sergey Alexandrovich Kryukov 6-Aug-12 18:36pm    
How about reading the manual? Asking such question is not an effective way.
--SA

PHP does nothing but reading or parsing HTTP request and generating HTTP response by writing headers (via header) and the body of response (via echo). In your case, the script parses HTTP request with the set of files uploaded with the "POST" HTTP method. The variable $_FILES is a superglobal predefined variable containing a collection of files. They are processed in the loop, copied to some location on the server. Please see:
http://php.net/manual/en/reserved.variables.files.php[^],
http://www.php.net/manual/en/language.variables.superglobals.php[^].

Everything you need to learn about PHP is here:
http://www.php.net/manual/en/index.php[^].

—SA
 
Share this answer
 
Adding With SA
your problem is with foreach loop:

first I hope that you understand php array exactly as it is. now:
there is two way of writing foreach statement
1.
foreach (array_expression as $value)
statement
2
foreach (array_expression as $key => $value)
statement

in case of 1:
take an example:
C++
$arr['number_one']='me';
$arr[2]='you';
$arr[5]='him';
//this is not evenly incremented array if we apply the first foreach then 
foreach($arr as $value)//here value would be the arr value that we are holding.
{
 echo $value."\n";
}

//the output would be
/*
me
you
him
*/

//now for the second way
foreach($arr as $key => $value)//here $key will be the index key of the array
{
 echo $key." => ".$value."\n";
}
//output would be:
/*
number_one => me
2 => you
3 => him
*/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900