Skip to content

Instantly share code, notes, and snippets.

@6LYTH3
Created November 30, 2012 15:06
Show Gist options
  • Save 6LYTH3/4176285 to your computer and use it in GitHub Desktop.
Save 6LYTH3/4176285 to your computer and use it in GitHub Desktop.
upload image file in php
<?php
if (isset($_FILES['image'])) {
$errors = array();
$allowed_ext = array('jpg','jpeg','png','gif');
$file_name = $_FILES['image']['name'];
$file_ext = strtolower(end(explode(".", $file_name)));
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
if (in_array($file_ext, $allowed_ext) === false ) {
$errors[] = 'Extenstion not allowed';
}
if ($file_size > 2097152) {
$errors[] = 'File size must be under 2mb';
}
if (empty($errors)) {
if(move_uploaded_file($file_tmp, 'images/'.$file_name)){
echo "File Uploaded";
}
}else{
foreach ($errors as $error) {
echo $error , '<br>';
}
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<p>
<input type="file" name="image" />
<input type="submit" value="Upload"/>
</p>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment