1

hello i starting an code to allow sending file via a form i create an input file field

<input type="file" name="'.$field_name.'[]" accept="'.$values_field[0].'" id="'.$field_id.'" '.$placeholder.' aria-label="'.$field_label.'" class="inputfile '.$required.'" '.$uploadmode.' style="margin:0" maxuploads = 2 >

i create this code

//upload attachement
        $files = $jinput->files->get($formid);
        if (isset($files))
        {
            foreach($files as $attachements) {
                foreach ($attachements as $file){
                // Import filesystem libraries. Perhaps not necessary, but does not hurt.
                jimport('joomla.filesystem.file');

                // Clean up filename to get rid of strange characters like spaces etc.
                $filename = JFile::makeSafe($file['name']);

                // Set up the source and destination of the file
                $src = $file['tmp_name'];
                $dest = JPATH_SITE . DS . "tmp" . DS . $filename;
                    // TODO: Add security checks. FIle extension and size maybe using joomla rules

                    if (JFile::upload($src, $dest))
                        {
                    $mailer->addAttachment($dest);
                        } 
                    else
                        {
                        $app->enqueueMessage(JText::_('FLEXI_FIELD_EMAIL_MESSAGE_SEND_ERROR'), 'error');
                        }
                }
            }
        }

        //Sendemail
        $send = $mailer->Send();

        //Message in front-end
        if ( $send !== true )
            {
                $app->enqueueMessage(JText::_('FLEXI_FIELD_EMAIL_MESSAGE_SEND_ERROR'), 'error');
                //Deleting file
                if (is_file($dest)) {
                JFile::delete($dest);
                } 
            } else {
                // Message sending
                $app->enqueueMessage(JText::_('FLEXI_FIELD_EMAIL_MESSAGE_SEND_SUCCESS'), 'message');
                //Deleting file
                if (is_file($dest)) {
                JFile::delete($dest);
                } 
            }
    }

i need some help for 2 questions, how is the best way to check file extension for better security ? can i use (and how) the joomla rules (mime and size files) what is the better solution for deleting files after sending without redo the foreach ?? thanks for any help !

6

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.