Core PHP

Upload a File and Get Its Properties

This PHP example program demonstrates how to upload a file and list its properties. The code is listed directly below as the file "UploadAndGetProperties.php". Below the code are several images that show what is displayed when the program is executed. The initial image shows the two buttons that first show up. Once the "Choose File" button is pressed, the file chooser window opens to allow the user to select a file as shown in the "Choose File" image. Once the user selects a file, like the "XoaX.txt" file shown here, and presses the "Open" button, the file name is displayed as shown in the "File Chosen" image. Finally, the output is displayed after the user clicks the "Submit Form" button.

UploadAndGetProperties.php

<?php
	if (IsSet($_POST['Submit']) && IsSet($_FILES['File'])) {
		$saFile = $_FILES['File'];
		// Run through the properties of the file and display its values.
		foreach ($saFile as $sKey => $sValue) {
			echo "&dollar;saFile[$sKey] = $saFile[$sKey]<br />";
    	}
	} else {
?>
	<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"
	  method="post" enctype="multipart/form-data">
		<input type="file" name="File" />
		<input type="submit" name="Submit" value="Submit form" />
	</form>
<?php
	}
?>
 

Output

$saFile[name] = XoaX.txt
$saFile[type] = text/plain
$saFile[tmp_name] = C:\Windows\Temp\php7B9A.tmp
$saFile[error] = 0
$saFile[size] = 2828
 

Initial

Initial
 

Choose File

Choose File
 

File Chosen

File Chosen
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.