doudiemei2013 2015-01-01 22:37
浏览 44
已采纳

是否有必要手动检查php.ini的文件大小?

So when uploading files in PHP I'm currently checking for errors in upload size against the php.ini file:

switch ($_FILES['file']['error']) {
    case UPLOAD_ERR_OK:
        break;
    case UPLOAD_ERR_NO_FILE:
        throw new RuntimeException('No file sent.');
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        throw new RuntimeException('Exceeded filesize limit.');
    default:
        throw new RuntimeException('Unknown errors.');
}

And then checking them manually:

$max_size = "10490000";
if ($_FILES['file']['size'] > $max_size) {
    throw new RuntimeException('Exceeded filesize limit.');
}

Is this second step necessary? Or is it sufficient just to check the errors.

Alternatively, if it is necessary, can I safely do something like this for the second check:

$max_size = (int)(ini_get('upload_max_filesize'));
  • 写回答

1条回答 默认 最新

  • duannao8450 2015-01-01 23:16
    关注

    (1) The second step is not necessary but you have to separate the UPLOAD_ERR_INI_SIZE case from UPLOAD_ERR_FORM_SIZE case because they have different meanings as the manual says http://php.net/manual/en/features.file-upload.errors.php

    (2) the value of the entry upload_max_filesize at php.ini file is written using this notation:
    XY: where X is an integer and Y is the unit, Y can be G(giga bytes), M (mega bytes)...etc, read the manual for more clarification http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes, so using (int)(ini_get('upload_max_filesize')); will not be safe all times and requires extra handling

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿