-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupload.php
More file actions
26 lines (24 loc) · 1.39 KB
/
upload.php
File metadata and controls
26 lines (24 loc) · 1.39 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
<?php
$teste = 0;
$max_filesize = 2097152; // Maximum filesize in BYTES.
$allowed_filetypes = array('.jpg','.jpeg','.gif','.png'); // These will be the types of file that will pass the validation.
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
$file_strip = str_replace(" ","_",$filename); //Strip out spaces in filename
$upload_path = 'uploads/'; //Set upload path
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes)) {
die('<div class="error">The file you attempted to upload is not allowed.</div>');
}
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) {
die('<div class="error">The file you attempted to upload is too large.</div>');
}
// Check if we can upload to the specified path, if not DIE and inform the user.
// Move the file if eveything checks out.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $file_strip)) {
echo '<div class="success">'. $file_strip .' uploaded successfully</div>'; // It worked.
} else {
echo '<div class="error">'. $file_strip .' was not uploaded. Please try again.</div>'; // It failed :(.
}
?>