Skip to content

Instantly share code, notes, and snippets.

@unaibamir
Last active September 4, 2018 19:18
Show Gist options
  • Save unaibamir/197efc06dad157d06cff50d426c15812 to your computer and use it in GitHub Desktop.
Save unaibamir/197efc06dad157d06cff50d426c15812 to your computer and use it in GitHub Desktop.
PHP File Uploading script
<?php
$currentDir = getcwd();
$uploadDirectory = "/uploads/";
$errors = []; // Store all foreseen and unforseen errors here
if (isset($_POST['submit']) && isset($_FILES['myfile'])) {
$fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
$fileTmpName = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
if (! in_array($fileExtension,$fileExtensions)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
}
if ($fileSize > 2000000) {
$errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "The file " . basename($fileName) . " has been uploaded";
} else {
echo "An error occurred somewhere. Try again or contact the admin";
}
} else {
foreach ($errors as $error) {
echo $error . "These are the errors" . "\n";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Upload with PHP</title>
</head>
<body>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
Upload a File:
<input type="file" name="myfile" id="fileToUpload">
<input type="submit" name="submit" value="Upload File Now" >
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment