Search code examples
phpfile-uploaduploadundefinedrtf

Uploading rtf file in php


I can upload other file without error. But whenever I tried to upload rtf file in php it fails. My code it below:

if(isset($_POST['pid'])){           
    if($_FILES['uploadname']['name']==''){
     //Failed
    }else{
    //upload the file
    }
}

HTML form:

<form method="post" enctype="multipart/form-data">
  <input type="file" accept=".csv,.doc,.pdf,.docx,.xls,.xlsx,.rtf,.txt, image/*" 
        name="uploadname" style="width:100%;">
   <input type="hidden" name="pid" value="<?php echo $id; ?>">
   <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>">
</form>

$max = 62914560 bytes

I got undefined index uploadname

The server log:

PHP Warning: POST Content-Length of 24783980 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

I upload the same file (resaved as doc format using msword) without problem. But when it comes to rtf it fails. I can upload other files like doc, docx, xls, xlsx, pdf, txt and all image files without problem. What could be the problem. I am using php 7.1.19 Thanks


Solution

  • See the documentation:

    The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too large and the transfer failed. Keep in mind: fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. It is merely a convenience feature for users on the client side of the application.

    You put that field after the file input, so it will be ignored.

    The PHP settings (on the server side) for maximum-size, however, cannot be fooled.

    … and it looks like your server-side configuration is set to a lower value anyway, so you need to increase that.