dongmo6937 2011-08-29 00:29
浏览 16

上传图片大小问题

I built a website which allow people to upload their photos using php, but the phots can't be successfully uploaded if the image size is too big, how can I avoid that problem, how can I make the people upload the image without the size limitation, could you show me some php code relevant to that if possible, thanks!

  • 写回答

1条回答 默认 最新

  • dqd22496 2011-08-29 00:35
    关注

    This is possible with .htaccess. You'll need to add the following lines to your file:

    <IfModule mod_php5.c>
        php_value upload_max_filesize 20M
    </IfModule> 
    

    Although it's probably better to increase the script execution time (so it doesn't time out waiting for the upload) and the maximum POST size, thus:

    <IfModule mod_php5.c>
        php_value upload_max_filesize 20M
        php_value post_max_size 20M
        php_value max_execution_time 120
    </IfModule> 
    

    This sets the maximum execution time to two minutes, which is a very long amount of time, however this is just an example.


    PHP solution (won't work - see comments)

    First, turn on error checking; this will let you know what's happened and where by putting this at the top of your script:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    

    You'll need to change the upload_max_filesize and the post_max_size declarations in your php.ini (assuming you're using Apache).

    Find a lines that look similar to this:

    upload_max_filesize = 2M
    post_max_size = 8M
    

    And change it to something like:

    upload_max_filesize = 20M
    post_max_size = 20M
    

    This sets the upload limit to 20MB (which is rather large, I must say). If you're on shared hosting, it's very unlikely they'll allow you to edit php.ini. In this case, you can use ini_set() like such:

    string ini_set('upload_max_filesize', '20M');
    string ini_set('post_max_size', '20M');
    

    This will set the upload limit to 20MB, or whatever you want to set it to.

    (Thank you to @Phil in the comments) Do note that upload_max_filesize must be less than or equal to post_max_size.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数