Upload, file type validation

Hey guys,

I have a file upload form which saves the uploaded file to a path, adds some random numbers and inserts the filename into a mysql DB.

I’m just not sure how to add file type validation into this form.

Could someone please help me?

[php]// Image Upload Script

$uploadDir = ‘images/’; //Image Upload Folder
if(isset($_POST[‘submit’])) {

$fileName = ( rand(1, 999).rand(1000,9999 ).rand(1, 999)."_". $_FILES[‘photo’][‘name’]);
$tmpName = $_FILES[‘photo’][‘tmp_name’];
$fileSize = $_FILES[‘photo’][‘size’];
$fileType = $_FILES[‘photo’][‘type’];
$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo “Error uploading file”;
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
} [/php]

This is kinda the long way to do it, but have a look at http://www.bitrepository.com/how-to-validate-an-image-upload.html

Sponsor our Newsletter | Privacy Policy | Terms of Service