gpt4 book ai didi

php - 如何使用 php 处理 htaccess 错误?

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:01 26 4
gpt4 key购买 nike

我在我的 php 脚本中使用了 ini_set('post_max_size',"2M") 来限制上传文件的大小。它没有用(你知道为什么吗?)

所以我将这些规则放在我的 htaccess 文件中:

php_value post_max_size 2M

php_value upload_max_filesize 2M

所以当我上传一个大于 2M 的文件时,我的 php 脚本会显示这个错误:

警告:15903708 字节的 POST 内容长度超出第 0 行未知中 2097152 字节的限制

我怎样才能以适当的方式处理这个错误? (类似于 try catch)。

注意:我不想隐藏这个错误。

最佳答案

在您的上传脚本中,您是否尝试过:

if ($_FILES['upfile']['size'] > 2000000) {
$error_message ="Exceeded filesize limit.";
}

在 php 中还有 try/catch 例如:

try{
//your upload script
}
catch(Exception $e){
$error_message = "File did not upload: ". $e->getMessage();
}

从这里你应该能够按照你想要的方式处理任何错误

另一种解决方案是自定义错误处理程序:

 set_error_handler("warning_handler", E_WARNING);

//your upload script

function warning_handler($errno, $errstr) {
// do someing with your error here
}


restore_error_handler(); // reinstates original error handling

关于php - 如何使用 php 处理 htaccess 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34543869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com