Page 1 of 1

how to upload multiple files with multiple file tags ???

Posted: Tue Nov 06, 2007 10:55 pm
by djdon11
HI Friends ...


I want to upload multiple files ...... in single form post
means i have to upload 8 files but it is not sure that how much files user will upload may be 8 may be 1 or whatever

right now i am coding like this for every file upload :->

Code: Select all

if(!empty($_FILES['file2']['name']))
			{
			
				$file2=$_FILES['file2']['name'];

				if($file2)
				{
					$pos=strpos($file2,".");
					$ext=substr($file2,$pos+1,strlen($file2));
					if($ext=="jpg" || $ext=="bmp" ||$ext =="gif" ||$ext =="png")
					{
						$dirpath="./projectimages/";
						if(!file_exists($dirpath))
							mkdir($dirpath,777);
						$uploadfile=$dirpath.$title."2.".$ext;
						if($file2<=$size)
						{
							if(move_uploaded_file($_FILES['file2']['tmp_name'],$uploadfile))
								$path2=$title."2.".$ext;
							
						}	
						else
							$message="Second file is too large";
					}
					else
						$message="Please upload only jpg, bmp, png or gif file";		
					
					
				}

			}	  				  
		if(!empty($_FILES['file3']['name']))
			{
			
				$file3=$_FILES['file3']['name'];

				if($file3)
				{
					$pos=strpos($file3,".");
					$ext=substr($file3,$pos+1,strlen($file3));
					if($ext=="jpg" || $ext=="bmp" ||$ext =="gif" ||$ext =="png")
					{
						$dirpath="./projectimages/";
						if(!file_exists($dirpath))
							mkdir($dirpath,777);
						$uploadfile=$dirpath.$title."3.".$ext;
						if($file3<=$size)
						{
							if(move_uploaded_file($_FILES['file3']['tmp_name'],$uploadfile))
								$path3=$title."3.".$ext;
							
						}	
						else
							$message="Third file is too large";
					}
					else
						$message="Please upload only jpg, bmp, png or gif file";		
					
					
				}

			}

i want some code which reduce this redundancy of the coding

can any body help me ???

Posted: Wed Nov 07, 2007 3:25 am
by CoderGoblin
As $_FILES is an array you can use, if the array is not empty, foreach to run through each uploaded file.

Posted: Wed Nov 07, 2007 4:45 pm
by feyd
Looping is often a solution to redundant code. So are functions/methods. They aren't mutually exclusive either.