Validations in php

Hopefully it now goes without saying (although I’ll do so anyway) that form-data validation is of the utmost importance, due to the possibility of users attempting to hack into your server.
In addition to maliciously formed input data, some of the things you also have to check are whether a file was actually received and, if so, whether the right type of data was sent. Taking all these things into account, previous example, index1.php, is a rewrite of index.php.

<!-- index1.php -->

<!DOCTYPE html> 
		<title>PHP Form Upload</title>

	<form method='post' action='index1.php' enctype='multipart/form-data'>
		Select a JPG, GIF, PNG or TIF File: 
		<input type='file' name='filename' size='10' /> 
		<input type='submit' value='Upload' />

if ($_FILES) {
	$name = $_FILES ['filename'] ['name'];
	switch ($_FILES ['filename'] ['type']) {
		case 'image/jpeg' :
			$ext = 'jpg';
		case 'image/gif' :
			$ext = 'gif';
		case 'image/png' :
			$ext = 'png';
		case 'image/tiff' :
			$ext = 'tif';
		default :
			$ext = '';
	if ($ext) {
		$n = "image.$ext";
		move_uploaded_file ( $_FILES ['filename'] ['tmp_name'], $n );
		echo "Uploaded image '$name' as '$n':<br />";
		echo "<img src='$n' />";
	} else
		echo "'$name' is not an accepted image file";
} else
	echo "No image has been uploaded";


You may also like...

No © Loop and Break