Skip to content

Instantly share code, notes, and snippets.

@hlorand
Created August 22, 2014 22:35
Show Gist options
  • Save hlorand/ba35f16dbf468788fee7 to your computer and use it in GitHub Desktop.
Save hlorand/ba35f16dbf468788fee7 to your computer and use it in GitHub Desktop.
Uploads image and converts to base64
<?php
if(isset($_FILES['image']))
{
$file_name = $_FILES['image']['name'];
$file_array = explode(".", $file_name);
$file_ext = strtolower(array_pop($file_array));
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = pathinfo($file_tmp, PATHINFO_EXTENSION);
$file_data = file_get_contents($file_tmp);
$base64 = 'data:image/' . $file_type . ';base64,' . base64_encode($file_data);
echo "Filename: " . $file_name . "</br>";
echo "Extension: " . $file_ext . "</br>";
echo "Size: " . floor($file_size/1000) . " kb</br>";
echo "Tmp location: " . $file_tmp . "</br>";
echo "Base64 data:<br>";
echo "<textarea rows=\"4\" cols=\"50\">" . $base64 ."</textarea><br>";
echo "<img width=\"320\" src=\"" . $base64 . "\">";
}
?>
<!doctype html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<p>
<input type="file" name="image" />
<input type="submit" value="Upload">
</p>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment