MyBB Community Forums

Full Version: error: Attachment file name is too long
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When uploading an attachment, if the filename is longer than the field length, I get an error. This can happen if the filename details photo content.
Quote:SQL Error: 1406 - Data too long for column 'filename' at row 1
Query:
                        INSERT
                        INTO mybb_attachments (pid,posthash,uid,filename,filetype,filesize,attachname,downloads,dateuploaded,thumbnail,visible)
                        VALUES (0,'82308f13b0ceb97b6b3d8988ef3af7a2','262','Marlene Dietrich, Jean Harlow, Joan Blondell (looking bored unknown ( Elizabeth Allan), Edward G. Robinson, and Dolores del Rio..jpg','image/jpeg',80341,'201910/post_262_1571487304_daacdaa987c4bdbfdf717ea2d58b462c.attach',0,1571487304,'201910/post_262_1571487304_daacdaa987c4bdbfdf717ea2d58b462c_thumb.jpg',1)


Error is backtraced to /inc/functions_upload.php line 764.

I have increased field length in table mybb_attachments / filename from varchar(120) to varchar(200). I hope this does not have unintended consequences. This option is better than allowing the field to be truncated by dropping STRICT_TRANS_TABLES from sql-mode.

However, a better solution would be validation of the filename length and generating an error message similar to what I would get if filesize is too great.

Would this work? Please suggest corrections to syntax.

438      // Check the filename length
439      if(strlen($attachment['filename']) > 120 != "")
440      {
441          $ret['error'] = $lang->sprintf($lang->error_filenamelength, htmlspecialchars_uni($attachment['name']), "120");
442          return $ret;
443      }


It should be obvious I'm attempting to modify an existing portion of code, and it should *really* be obvious I am not experienced. But I am always willing to learn. Smile

Where would I insert the new error message?
test
Hi,

Thank you for your report. We have pushed this issue to our Github repository for further analysis where you can track our commits and progress with fixing this bug. Discussions regarding this bug may also take place there too.

Follow this link to visit the issue on Github: https://github.com/mybb/mybb/issues/3820

Thanks for contributing to MyBB!

Regards,
The MyBB Group
Increasing the column length is the most applicable way, IMO, compared to changing MySQL's strict mode or forcing user to shorten the filename. The limits of filename length on Most OSes should not exceed 256, via wiki, but I don't know how Unicode is handled, whether the filename length is counted by character or byte.

According to https://en.wikipedia.org/wiki/Comparison...ems#Limits , some counts by byte, some by character.
This might not be the best way to do it, but here's how I would add an error message (starting at line 739 of /inc/functions_upload.php):
		foreach($attachments as $FILE)
		{
			if(!empty($FILE['name']) && !empty($FILE['type']))
			{
				if($FILE['size'] > 0)
				{
					$filename = $db->escape_string($FILE['name']);
					$exists = $aid[$filename];
                    if(strlen($filename) < 120 != "")
                    {
                        $update_attachment = false;
                        if($action == "editpost")
                        {
                            if($exists && $mybb->get_input('updateattachment') && ($mybb->usergroup['caneditattachments'] || $forumpermissions['caneditattachments']))
                            {
                                $update_attachment = true;
                            }
                        }
                        else
                        {
                            if($exists && $mybb->get_input('updateattachment'))
                            {
                                $update_attachment = true;
                            }
                        }

                        $attachedfile = upload_attachment($FILE, $update_attachment);

                        if(!empty($attachedfile['error']))
                        {
                            $ret['errors'][] = $attachedfile['error'];
                            $mybb->input['action'] = $action;
                        }
                        
                    } else {
                        $ret['errors'][] = $lang->sprintf($lang->error_filenamelength, htmlspecialchars_uni($attachment['name']), "120");
                        $mybb->input['action'] = $action;
                    }
				}
				else
				{
					$ret['errors'][] = $lang->sprintf($lang->error_uploadempty, htmlspecialchars_uni($FILE['name']));
					$mybb->input['action'] = $action;
				}
			}
		}
	}