<html>
<head>
<title>Simple File Upload Script in PHP</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<?php
// check the file is uploaded or not
if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {
// Determine the file location
$newname = dirname(__FILE__) . '/' .basename($_FILES['attachment']['name']);
if($_FILES['attachment']['size'] > 2097152) {
$errors[]='File size must be excately 2 MB';
}
// Check Allowed File Types
$file_ext=strtolower(end(explode('.',$_FILES['attachment']['name'])));
$extensions= array("pdf","doc","mp4","jpg");
if(in_array($file_ext,$extensions)=== false){
$errors[]="File extension not allowed, please choose a PDF, DOC, DOCX file.";
}
if(empty($errors)==true){
// Move the file from temporary location to determined location
if (!(move_uploaded_file($_FILES['attachment']['tmp_name'], $newname))) {
echo "<p>ERROR: A problem occurred during file upload!</p>\n";
} else {
echo "<p>The file has been saved as: {$newname}</p>\n";
}
}
else{
print_r($errors);
}
}
?>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-md-3 control-label">Upload a file (PDF, DOC, DOCX, MP4, JPG)</label>
<div class="col-md-6">
<input type="file" name="attachment" class="form-control-file" />
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-md-offset-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</body>
</html>
Comments