Skip to content

Instantly share code, notes, and snippets.

@jamsesso
Created January 18, 2010 01:55
Show Gist options
  • Save jamsesso/279726 to your computer and use it in GitHub Desktop.
Save jamsesso/279726 to your computer and use it in GitHub Desktop.
<?php
function upload($file) {
$allowed = array("image/png", "image/jpg", "image/jpeg", "image/gif");
$type = $file['type'];
$extension = end(explode("/", $type));
$destination = sha1(time().rand().md5($file['name'])).".".$extension;
$directory = "./uploads/";
$limit = 1024000;
$success = "Your photo has been uploaded!";
if(!in_array($type, $allowed)) {
$error = "The file you are trying to upload is not an allowed file type. Please upload only PNG's, JPEG's, or GIF's.";
}
if($file['size'] > $limit) {
$error = "The photo you are uploading exceeds our maximum size of 1000KB. Please compress the photo or try uploading another one.";
}
if(!isset($error)) {
move_uploaded_file($file['tmp_name'], $directory.$destination) or die("Cannot upload file.");
echo $success;
} else {
echo $error;
}
}
//USAGE -------------
/*
upload($_FILES['photo']); <- this will verify the file.
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment