Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
限制WordPress中仅上传图像的大小_Wordpress_Image_Upload_Image Upload - Fatal编程技术网

限制WordPress中仅上传图像的大小

限制WordPress中仅上传图像的大小,wordpress,image,upload,image-upload,Wordpress,Image,Upload,Image Upload,如何严格限制仅在WordPress中上传图像的大小?如果您转到设置页面,它将告诉您上载文件类型和最大上载大小。我希望对除图像以外的所有图像保持最大上载大小一致。我认为这是不可能的,因为您无法在运行时更改上载\u max\u文件大小。检查。它说,upload\u max\u filesize是 否则你可以用jQuery/JS检查文件类型,改变upload\u max\u filesize,post\u max\u size,memory\u limit,或者用AJAX做任何适合你需要的事情(在我看

如何严格限制仅在WordPress中上传图像的大小?如果您转到设置页面,它将告诉您上载文件类型和最大上载大小。我希望对除图像以外的所有图像保持最大上载大小一致。

我认为这是不可能的,因为您无法在运行时更改
上载\u max\u文件大小。检查。它说,
upload\u max\u filesize

否则你可以用jQuery/JS检查文件类型,改变
upload\u max\u filesize
post\u max\u size
memory\u limit
,或者用AJAX做任何适合你需要的事情(在我看来这有点安全漏洞),然后上传它


由于您无法在运行时更改
上载\u max\u filesize
,因此检查上载的文件是否为图像或其他任何内容都没有意义,因为所有文件的文件大小都相同。上传不是指移动上传的文件()
功能,而是指从客户端到临时服务器文件夹的文件传输。

我认为这是不可能的,因为您无法在运行时更改
上传的最大文件大小。检查。它说,
upload\u max\u filesize

否则你可以用jQuery/JS检查文件类型,改变
upload\u max\u filesize
post\u max\u size
memory\u limit
,或者用AJAX做任何适合你需要的事情(在我看来这有点安全漏洞),然后上传它


由于您无法在运行时更改
上载\u max\u filesize
,因此检查上载的文件是否为图像或其他任何内容都没有意义,因为所有文件的文件大小都相同。上传不是指移动上传的文件()
功能,而是指从客户端到临时服务器文件夹的文件传输。

我知道这个问题有点老了,但我最近不得不为一个项目做这个,也许它可以帮助别人

TL;DR

functions.php中使用它

/**
 * Limit the file size for images upload
 *
 * @param $file
 * @return mixed
 */
function filter_image_pre_upload($file)
{
    $allowed_types = ['image/jpeg', 'image/png'];

    // 3 MB.
    $max_allowed_size = 3000 * 1024;

    if (in_array($file['type'], $allowed_types)) {
        if ($file['size'] > $max_allowed_size) {
            $file['error'] = 'Please reduce the size of your image to 3 Mb or less before uploading it.';
        }
    }

    return $file;
}

add_filter('wp_handle_upload_prefilter', 'filter_image_pre_upload', 20);
详细信息:

您必须连接到wp\u handle\u upload\u prefilter过滤器,该过滤器允许您修改即将上载的文件数组(但在将其复制到最终位置之前)

您将收到一个
$file
数组,您需要的键是大小、类型、错误

$allowed\u types
数组中,您将添加想要允许的类型

$max\u allowed\u size
中,您将设置要允许的最大大小(该值以字节为单位)

然后验证上载的文件是否符合您的要求,如果不符合,则添加
$file['error']

在WordPress代码中,如果
$file['error']
与0不同,则表示该文件是一个显示错误的字符串,并阻止上载该文件


希望这有帮助

我知道这个问题有点老了,但我最近不得不为一个项目做这个,也许它可以帮助别人

TL;DR

functions.php中使用它

/**
 * Limit the file size for images upload
 *
 * @param $file
 * @return mixed
 */
function filter_image_pre_upload($file)
{
    $allowed_types = ['image/jpeg', 'image/png'];

    // 3 MB.
    $max_allowed_size = 3000 * 1024;

    if (in_array($file['type'], $allowed_types)) {
        if ($file['size'] > $max_allowed_size) {
            $file['error'] = 'Please reduce the size of your image to 3 Mb or less before uploading it.';
        }
    }

    return $file;
}

add_filter('wp_handle_upload_prefilter', 'filter_image_pre_upload', 20);
详细信息:

您必须连接到wp\u handle\u upload\u prefilter过滤器,该过滤器允许您修改即将上载的文件数组(但在将其复制到最终位置之前)

您将收到一个
$file
数组,您需要的键是大小、类型、错误

$allowed\u types
数组中,您将添加想要允许的类型

$max\u allowed\u size
中,您将设置要允许的最大大小(该值以字节为单位)

然后验证上载的文件是否符合您的要求,如果不符合,则添加
$file['error']

在WordPress代码中,如果
$file['error']
与0不同,则表示该文件是一个显示错误的字符串,并阻止上载该文件

希望这有帮助