Skip to content

Instantly share code, notes, and snippets.

@garyrozanc
Created January 6, 2021 02:32
Show Gist options
  • Select an option

  • Save garyrozanc/96439a6d182107773912d118d2c5b387 to your computer and use it in GitHub Desktop.

Select an option

Save garyrozanc/96439a6d182107773912d118d2c5b387 to your computer and use it in GitHub Desktop.
<?php
$rand = substr(md5(time()),0,4);
$question = $_POST['question'];
function getext($img){
$name = strtolower($img);
$data = explode(".", $name);
$ext = count($data) -1;
return $data[$ext];
}
if(isset($_FILES)){
$errors = array();
$maxsize = 2097152;
$allowed = array('gif','jpg','jpeg','png','webp','woff','woff2');
$ext = getext($_FILES['file1']['name']);
$size = $_FILES['file1']['size'];
if ($question !== '4') {
$errors[] = 'You got 2 + 2 wrong.';
}
if(!in_array($ext, $allowed)) {
//if((!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"]))) {
$errors[] = 'Invalid file type. Only GIF, JPG, PNG, WEBP, WOFF, and WOFF2 types are accepted.';
}
//if($size > $maxsize) {
//if(($_FILES['file1']['size'] <= $maxsize) || ($_FILES["file1"]["size"] == 0)) {
if(($_FILES['file1']['size'] >= $maxsize) || ($_FILES["file1"]["size"] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}
if(count($errors) === 0) {
$name = $_FILES['file1']['name'];
$name = pathinfo($name, PATHINFO_FILENAME).".".substr(md5(time()),0,4).".".$ext;
$uploadFileDir = './uploads/';
move_uploaded_file($_FILES['file1']['tmp_name'], $uploadFileDir.$name);
echo "<span class=\"alert-green\">Your file has been uploaded.</span>";
} else {
foreach($errors as $error) {
echo "<p>".$error."</p>";
}
die(); //Ensure no more processing is done
}
}
?>
@garyrozanc
Copy link
Author

Here are the errors.

2021/01/05 21:28:59 [error] 922#922: *3 FastCGI sent in stderr: "PHP message: PHP Warning: POST Content-Length of 8419805 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
PHP message: PHP Notice: Undefined index: question in /var/www/garyrozanc.com/students/upload-php-test-03/upload.php on line 3
PHP message: PHP Notice: Undefined index: file1 in /var/www/garyrozanc.com/students/upload-php-test-03/upload.php on line 16
PHP message: PHP Notice: Undefined index: file1 in /var/www/garyrozanc.com/students/upload-php-test-03/upload.php on line 17
PHP message: PHP Notice: Undefined index: file1 in /var/www/garyrozanc.com/students/upload-php-test-03/upload.php on line 30
PHP message: PHP Notice: Undefined index: file1 in /var/www/garyrozanc.com/students/upload-php-test-03/upload.php on line 30" while reading response header from upstream, client: 86.106.121.248, server: garyrozanc.com, request: "POST /students/upload-php-test-03/upload.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "garyrozanc.com", referrer: "https://garyrozanc.com/students/upload-php-test-03/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment