如何限制要在WordPress中上传的文件类型?
How to limit file type to be upload in WordPress?
我有一个带有表单的自定义页面模板,该网站的任何访问者都可以上传文件。现在,我想限制将要上传的文件类型(仅限 docx、doc 和 pdf),并且我将文件大小限制为 2MB。
如何做到这一点?我已经有用户允许上传的功能,但我不知道如何限制允许上传的文件类型。请帮助我。
现在可以限制文件了,但是奇怪的是为什么文件太大了还保存了。如何解决这个问题?
PHP 在自定义页面模板中
if(isset($_POST['submit'])){
$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : '';
$middleName = isset($_POST['middleName']) ? $_POST['middleName'] : '';
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$mobile = isset($_POST['mobile']) ? $_POST['mobile'] : '';
$locations = isset($_POST['locations_list']) ? $_POST['locations_list'] : '';
$position = isset($_POST['position']) ? $_POST['position'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
if( ! empty($_FILES)){
$file=$_FILES['file'];
$attachment_id = upload_user_file($file);
}
if ($_FILES["file"]["size"] > 1048576){
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
}
PHP 在 functions.php
function upload_user_file($file = array()){
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if(isset($file_return['error']) || isset($file_return['upload_error_handler'])){
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH . 'wp-admin/includes/file.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
if(0 < intval($attachment_id)){
return $attachment_id;
}
}
return false;
}
只需检查正在上传的文件的MIME类型和大小,就可以根据文件类型限制大小
To check the file type you refer this
To check the file size before uploading
如果你google它你可以获得更多的解决方案
我有一个带有表单的自定义页面模板,该网站的任何访问者都可以上传文件。现在,我想限制将要上传的文件类型(仅限 docx、doc 和 pdf),并且我将文件大小限制为 2MB。
如何做到这一点?我已经有用户允许上传的功能,但我不知道如何限制允许上传的文件类型。请帮助我。
现在可以限制文件了,但是奇怪的是为什么文件太大了还保存了。如何解决这个问题?
PHP 在自定义页面模板中
if(isset($_POST['submit'])){
$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : '';
$middleName = isset($_POST['middleName']) ? $_POST['middleName'] : '';
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$mobile = isset($_POST['mobile']) ? $_POST['mobile'] : '';
$locations = isset($_POST['locations_list']) ? $_POST['locations_list'] : '';
$position = isset($_POST['position']) ? $_POST['position'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
if( ! empty($_FILES)){
$file=$_FILES['file'];
$attachment_id = upload_user_file($file);
}
if ($_FILES["file"]["size"] > 1048576){
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
}
PHP 在 functions.php
function upload_user_file($file = array()){
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if(isset($file_return['error']) || isset($file_return['upload_error_handler'])){
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH . 'wp-admin/includes/file.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
if(0 < intval($attachment_id)){
return $attachment_id;
}
}
return false;
}
只需检查正在上传的文件的MIME类型和大小,就可以根据文件类型限制大小
To check the file type you refer this
To check the file size before uploading
如果你google它你可以获得更多的解决方案