Created
February 1, 2015 05:45
-
-
Save rajankur/1194fbf284041245722f to your computer and use it in GitHub Desktop.
PHP: file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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