Skip to content

Instantly share code, notes, and snippets.

@yonseo
Last active March 2, 2018 22:43
Show Gist options
  • Save yonseo/4fd553d573215be09c425530c9a6d962 to your computer and use it in GitHub Desktop.
Save yonseo/4fd553d573215be09c425530c9a6d962 to your computer and use it in GitHub Desktop.
upload php
<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<legend>upload image:</legend>
<label for="photo">Filename:</label>
<input type="file" name="photo"/>
<input type="submit" name="upload" value="upload"/>
</form>
</body>
</html>
<?php
//includes
include "../../config.php";
if(isset($_POST['upload'])){
$image = $_FILES['photo'];
$imageName = $_FILES['photo']['name'];
$imageTmpName = $_FILES['photo']['tmp_name'];
$imageSize = $_FILES['photo']['size'];
$imageError = $_FILES['photo']['error'];
$imageType = $_FILES['photo']['type'];
$imageExt = explode('.', $imageName);
// image extension type
$imageActualExt = strtolower(end($imageExt));
//allowed extension types
$allowed = array('jpg','jpeg','png','pdf');
//check image type before upload
if (in_array($imageActualExt, $allowed)){
// if errors appear
if ($imageError === 0){
//check image size
if ($imageSize < 1000000){
// create a unique file name
$fileNameNew = uniqid('', true).".".$imageActualExt;
// upload to directory
$imageDir = __DIR__.'/../../uploads/images/feature/';
$fileDestination = $imageDir.$fileNameNew;
//move file from temporary location to directory
move_uploaded_file($imageTmpName, $fileDestination);
//query code here
// go back to index.php page
header("Location: $admin_url/view/pages/index.php?uploadsuccess");
}else {
echo "Your file is too big!";
}//end image size check
}else {
echo "There was an error uploading your file.";
}// end image error check
} else {
echo "You cannot upload files of this type!";
}// end image type check
}// end if(isset)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment