Skip to content

Instantly share code, notes, and snippets.

@spinsch
Created August 15, 2017 14:57
Show Gist options
  • Save spinsch/e6a17d9f0fddcf0607e58e515cca829e to your computer and use it in GitHub Desktop.
Save spinsch/e6a17d9f0fddcf0607e58e515cca829e to your computer and use it in GitHub Desktop.
image upload test for php via post
<?php
function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
$debug = array(
'error' => '',
'data' => array()
);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (is_uploaded_file($_FILES['image_upload']['tmp_name'])) {
$debug['data']['files'] = $_FILES;
if ($_FILES["image_upload"]["error"] == UPLOAD_ERR_OK) {
$info = getimagesize($_FILES['image_upload']['tmp_name']);
if ($info === FALSE) {
$debug['error'] = "Unable to determine image type of uploaded file";
} else {
$debug['data']['image'] = $info;
}
} else {
$debug['error'] = "upload has an error";
}
} else {
$debug['error'] = "is not uploaded file";
}
$debug['data']['config'] = array(
ini_get('post_max_size'),
ini_get('upload_max_filesize'),
);
}
?>
<pre><?php print_r($debug) ?></pre>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo return_bytes(ini_get('post_max_size')); ?>" />
<input name="image_upload" type="file" />
<input type="submit" value="send" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment