-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
74 lines (69 loc) · 2.71 KB
/
upload.php
File metadata and controls
74 lines (69 loc) · 2.71 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
$target_name = basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_name,PATHINFO_EXTENSION));
$target_dir = "uploads/$name." . $imageFileType;
$target_file = $target_dir;
$uploadOk = 1;
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
include("deleteall.php");
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$lines = file('names.txt');
$rand = $lines[array_rand($lines)];
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
$content = "<html>\n<head>\n<script>\nfunction getVote(int) {\n var xmlhttp=new XMLHttpRequest();\n xmlhttp.onreadystatechange=function() {\n if (this.readyState==4 && this.status==200) {\n document.getElementById(\"poll\").innerHTML=this.responseText;\n }\n }\n xmlhttp.open(\"GET\",\"poll_vote.php?vote=\"+int,true);\n xmlhttp.send();\n}\n</script>\n</head>\n<body>\n<div id=\"poll\">\n<h3>Who is it?</h3>\n<img src=\"uploads/$name.$imageFileType\"/>\n<form>\n$name: <input type=\"radio\" name=\"vote\" value=\"0\" onclick=\"getVote(this.value)\"><br>\n$rand: <input type=\"radio\" name=\"vote\" value=\"1\" onclick=\"getVote(this.value)\">\n</form>\n<br>\n";
$fh = fopen('index.php', 'w');
fwrite($fh, $content);
fclose($fh);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>