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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真