Skip to content

Instantly share code, notes, and snippets.

@rajankur
Created February 1, 2015 05:45
Show Gist options
  • Save rajankur/1194fbf284041245722f to your computer and use it in GitHub Desktop.
Save rajankur/1194fbf284041245722f to your computer and use it in GitHub Desktop.
PHP: file upload
<?php
if(isset($_FILES["file"]))
{
$ext_allowed = array("jpg","png"); // add allowed extensions here
$dir = "uploads"; // Add Directory for uploads here
$name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$newname = substr(md5(rand(1231,483798489)),0,6)."_".strtotime(date('Y-m-d H:i:s'));
$newname = $newname.".$ext"; // The final name of the uploaded file, insert this name into your database column for future use.
$movepath = $dir."/".$newname;
if(in_array($ext, $ext_allowed))
{
move_uploaded_file($_FILES["file"]["tmp_name"],$movepath);
$code = "succ=done";
}
else
{
$code = "err=filetype"; // this error signifies that the uploaded filetype is not allowed.
}
header("location:upload.php?$code"); // change your redirect page here
ob_end_flush();
exit();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment