index php html head title File upload title head body form action uplo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//index.php
<html>
<head>
<title>File upload</title>
</head>
<body>
<form action="./upload.php" method="post" enctype="multipart/form-data">
<input type="text" name="numbers" />
Upload file: <input type="file" name="file" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
//upload.php
<?php
$uploadsPath = 'uploads/';
$valid_file = true;
$fileSize = 1024000;
if($_FILES['file']['name']) {
if(!$_FILES['file']['error']) {
$newFileName = strtolower($_FILES['file']['name']);
// 1mb
if($_FILES['file']['size'] > ($fileSize)) {
$valid_file = false;
$message = 'The file size is to large';
}
if($valid_file) {
move_uploaded_file($_FILES['file']['tmp_name'], $uploadsPath . $newFileName);
$message = 'The file has been uploaded';
}
} else {
$message = 'Error: '.$_FILES['file']['error'];
}
echo $message;
}