Skip to content

Instantly share code, notes, and snippets.

@rogeruiz
Created October 23, 2012 15:33
Show Gist options
  • Save rogeruiz/3939495 to your computer and use it in GitHub Desktop.
Save rogeruiz/3939495 to your computer and use it in GitHub Desktop.
A simple PHP image uploader
<?php
// Max size in KB
define('MAX_SIZE', '100');
$errors = 0;
if (isset($_POST['Submit'])) {
$image = $_FILES['image']['name'];
if ($image) {
$fileName = strtolower(pathinfo($image, PATHINFO_FILENAME));
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (($ext != 'jpg') && ($ext != 'jpeg') && ($ext != 'png') && $ext != 'gif') {
echo '<h1>Unknown extension</h1>';
$errors = 1;
} else {
$imageName = time().'.'.$ext;
$newName = 'images/'.$imageName;
$copied = copy($image, $newName);
if (!$copied) {
echo '<h1>Copy Unsuccessful</h1>';
$errors = 1;
}
}
}
}
if (isset($_POST['Submit']) && !$errors) {
echo '<h1>File Uploaded Sucessfully</h1>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment