Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yehgdotnet/57ad79a24356461b954e5964271da584 to your computer and use it in GitHub Desktop.
Save yehgdotnet/57ad79a24356461b954e5964271da584 to your computer and use it in GitHub Desktop.
PHP: Verifying uploaded file - pathinfo($file["name"], PATHINFO_EXTENSION);
<?php
function validFileType($file){
if($file && $file['tmp_name'] !=''){
$ext_allow_type = array('xlsx');
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
echo "Extension is ". $ext."<br>";
if(in_array($ext, $ext_allow_type) != FALSE) {
return true;
}
return false;
}
return true;
}
if(isset($_POST["submit"])) {
$uploaded_file = $_FILES["fileToUpload"]["tmp_name"];
$check = validFileType($_FILES["fileToUpload"]);
if($check !== false) {
echo "<h2 style=color:green>File is approved!</h2><br/>";
$uploadOk = 1;
} else {
echo "<h2 style=color:red>File is not approved!</h2><br/>";
$uploadOk = 0;
}
}
?><!DOCTYPE html>
<html>
<body>
<form action="file-upload1.php" method="post" enctype="multipart/form-data">
Select file to upload: &nbsp;&nbsp;
<input type="file" name="fileToUpload" id="fileToUpload"> &nbsp;&nbsp;
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment