test.php
<?php include "library/Upload.php";
$upload = new Upload();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<p><br><br></p>
<div class="container">
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'])
{
$uploadFile = $upload->UploadFile();
}
?>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Upload.php
<?php
include 'Database.php';
class Upload
{
private $db;
public function __construct()
{
$this->db = new Database();
}
public function UploadFile()
{
$name = $_FILES['userfile']['name'];
$type = $_FILES['userfile']['type'];
$size = $_FILES['userfile']['size'];
$content = $_FILES['userfile']['tmp_name'];
$sql = "INSERT INTO upload(name, type, size, content) VALUES(:name, :type, :size, :content)";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':name', $name);
$query->bindValue(':type', $type);
$query->bindValue(':size', $size);
$query->bindValue(':content', $content);
$result = $query->execute();
if ($result) {
$message = "<div class='alert alert-success'>Շնորհակալություն, դուք գրանցվեցիք Color School կայքում:</div>";
return $message;
} else {
$message = "<div class='alert alert-danger'><strong>Սխալ՝ </strong>կներեք, առաջացավ խնդիրներ ձեր տվյալները տեղադրելուց:</div>";
return $message;
}
}
}