You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Array in HTML5 is useful when upload files with multi selection on file management dialog. In the PHP, you will be able to manage uploaded files easily by index.

form.php
<html>
	<head>
		<title>PHP upload file demo</title>
	</head>
	<body>
		<form method="post" enctype="multipart/form-data" name="formUploadFile" action="submit_upload.php">		
			<label>Select single file to upload:</label>
			<input type="file" name="files[]" multiple="multiple" />
			<input type="submit" value="Upload File" name="btnSubmit"/>
		</form>		
	</body>
</html>



submit_upload.php
<?php

if(isset($_POST["btnSubmit"]))
{
	$errors = array();
	$uploadedFiles = array();
	$extension = array("jpeg","jpg","png","gif");
	$UploadFolder = "upload";
				
	$counter = 0;

	/*
		
		$_FILES["files"] Array
		(
		    [name] => Array
		        (
		            [0] => channel_representative_image_BilliadsTV_1920x1080.jpg
		            [1] => channel_representative_image_honey_1920x1080.jpeg
		            [2] => channel_representative_image_idap_1920x1080.jpeg
		            [3] => channel_representative_image_mubeat_1920x1080.jpg
		            [4] => channel_representative_image_muKbang_1920x1080.jpg
		        )

		    [type] => Array
		        (
		            [0] => image/jpeg
		            [1] => image/jpeg
		            [2] => image/jpeg
		            [3] => image/jpeg
		            [4] => image/jpeg
		        )

		    [tmp_name] => Array
		        (
		            [0] => /tmp/php4Mfw5C
		            [1] => /tmp/phpfSF3XV
		            [2] => /tmp/phpyzCFEf
		            [3] => /tmp/phpDRHBnz
		            [4] => /tmp/phpSzSCaT
		        )

		    [error] => Array
		        (
		            [0] => 0
		            [1] => 0
		            [2] => 0
		            [3] => 0
		            [4] => 0
		        )

		    [size] => Array
		        (
		            [0] => 1707487
		            [1] => 2494888
		            [2] => 1327434
		            [3] => 1912188
		            [4] => 1806818
		        )

		)
	*/				

	foreach($_FILES["files"]["tmp_name"] as $i=>$tmp_name)
	{
		$temp = $_FILES["files"]["tmp_name"][$i];
		$name = $_FILES["files"]["name"][$i];
					
		if(empty($temp))
		{
			break;
		}
					
		$counter++;
		$UploadOk = true;
					
		$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
		if(in_array($ext, $extension) == false)
		{
			$UploadOk = false;
			array_push($errors, $name." is invalid file type.");
		}
					
		if(file_exists($UploadFolder."/".$name) == true)
		{
			$UploadOk = false;
			array_push($errors, $name." file is already exist.");
		}
					
		if($UploadOk == true)
		{
			if (!file_exists($UploadFolder)) mkdir($UploadFolder);
			move_uploaded_file($temp, $UploadFolder."/".$name);
			array_push($uploadedFiles, $name);
		}
	}
				
	if($counter>0)
	{
		if(count($errors)>0)
		{
			echo "<b>Errors:</b>";
			echo "<br/><ul>";
			foreach($errors as $error)
			{
				echo "<li>".$error."</li>";
			}
			echo "</ul><br/>";
		}
					
		if(count($uploadedFiles)>0)
		{
			echo "<b>Uploaded Files:</b>";
			echo "<br/><ul>";
			foreach($uploadedFiles as $fileName)
			{
				echo "<li>".$fileName."</li>";
			}
			echo "</ul><br/>";
			
			echo count($uploadedFiles)." file(s) are successfully uploaded.";
		}								
	}
	else
	{
		echo "Please, Select file(s) to upload.";
	}
}
?>


  • No labels