Hier ist der Code, der das Hochladen von Bildern in das Stammverzeichnis der Website ermöglicht. Es ist eine Untermenüseite im Admin-Bereich, die eine Funktion aufruft, und der Funktionskörper ist unten:
<div class="wrap">
<h2>Upload files</h2><br><br><br>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" class="button" name="fileToUpload" id="fileToUpload">
<input type="submit" class="button button-primary" value="Upload File" name="submit">
</form>
</div>
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_dir = get_home_path();//wp_upload_dir();
$target_file = get_home_path() . "https://stackoverflow.com/" . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>File is an image</strong>
</div>
<?php
$uploadOk = 1;
} else {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>File is not an image</strong>
</div>
<?php
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>File already exists.</strong>
</div>
<?php
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>Your file is too large.</strong>
</div>
<?php
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=
"jpeg"
&& $imageFileType != "gif" ) {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</strong>
</div>
<?php
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>Sorry, your file was not uploaded.</strong>
</div>
<?php
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
?>
<div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
<strong>The file has been uploaded.</strong>
</div>
<?php
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
Ich möchte diese Dateitypen anstelle von Bildern zulassen: .xml, .rar, .zip, .txt. Ich habe versucht, die Funktion zu ändern getimagesize()
mit der Funktion getfilesize()
und um die Dateierweiterungen unten zu ändern // Allow certain file formats
Kommentar. Aber es funktioniert nicht. Haben Sie eine Idee, was ich tun soll, um das Hochladen dieser Dateitypen anstelle von Dateitypen zuzulassen, die derzeit zum Hochladen aktiviert sind?