我想启用直接将文件上传到根目录的功能。支持的文件扩展名应为:.txt,.rar,.xml,.zip。我正在尝试使用w3schools的代码来做到这一点,因为我是php和wordpress插件开发的初学者。插件正常工作,左侧管理栏上有一个菜单图标,管理页面上有一个上传选项,但是当我“上传”文件时,uploads文件夹中没有文件(我首先尝试这样做代码在w3schools上。代码是:
HTML
<div class="wrap">
<h2>Test file upload page</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</div>
和 PHP 文件(upload.php)
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=
"jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been
uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
我知道这不是一个好方法,并且有Wordpress函数可以执行我想做的事情,但是我只是想尝试这种方法...如果您知道更好的方法,我会谢谢你睁开眼睛。另外,我还有一个问题:可以在本地上传文件吗?我的意思是,我正在使用MAMP在localhost上进行所有操作,这是出于练习目的。
我没有编辑任何代码来支持我想要的文件扩展名,因为我首先想测试一下它是否适用于图像文件,但不是。
编辑1:
我还看到在php.ini文件中,file_uploads指令应设置为On:
file_uploads = On
但是,wordpress目录中没有php.ini文件。有没有办法用wordpress插件编辑php.ini文件?也许这就是问题所在。只是猜测。
答案 0 :(得分:1)
您的代码存在以下问题:
不需要upload.php
文件,所有内容都应该放在一个文件中,在本例中为您的主插件文件。
由于没有upload.php
文件,因此表单操作应为空。
PHP代码中的所有内容都应位于if(isset($_POST["submit"])) { }
中,因为如果未提交任何内容,则无需执行上传脚本中的任何操作。
您的$target_dir
应该是absolute path,例如:
/home/user/public_html/wp-content/
/Users/username/Sites/wp/wp-admin
这是一个基于您发布的代码的小示例,我使用wp_upload_dir
设置目标目录。在我的测试中,图像已上传到http://localhost/wp-content/2018/10/image.png
。
<?php
/**
* Plugin Name: (SO) Testing upload
*/
add_action('admin_menu', function () {
add_menu_page(
'Upload',
'Upload',
'read',
'so_upload',
'so_upload_callback',
null,
6 // position, just after Posts
);
});
function so_upload_callback()
{
?>
<div class="wrap">
<h2>Test file upload page</h2>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</div>
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_dir = wp_upload_dir();
$target_file = $target_dir['path'] . '/' . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=
"jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been
uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
}
请注意,以这种方式上传的文件不会显示在WP媒体库中。另外,如果代码是真实的,则表单需要一个随机数字段来确保安全性(wp_nonce_field
)。
答案 1 :(得分:0)
第一次编辑: 您无法通过PHP语言(Wordpress)编辑php.ini文件。
您的文件上传问题: 是的,文件上传将在本地进行。
为了向您指出正确的方向,我将使用堆栈溢出时先前回答的帖子中的代码,例如:
Wordpress custom file upload in page
PS:始终使用php日志调试PHP