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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

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

Post 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 ???
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

As $_FILES is an array you can use, if the array is not empty, foreach to run through each uploaded file.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Looping is often a solution to redundant code. So are functions/methods. They aren't mutually exclusive either.
Post Reply