1

I recently added my own form to the Magento frontend. The form contains a file type input, an used the file uploader class to save the file. But I was wondering how save this class is?

I already changed the name, checked the file size (not in code below) and checked the file extension. But I'm wondering if that is enough.

Here is my code (I'm checking file size in an earlier function):

private function uploadFile($id) {
    $uploader = $this->_fileUploaderFactory->create(['fileId' => $id]);
    $uploader->setAllowRenameFiles(TRUE);
    $uploader->setFilesDispersion(TRUE);
    $uploader->setAllowCreateFolders(TRUE);
    $uploader->setAllowedExtensions(Helper::ALLOWED_FILE_UPLOADS);

    $path = $this->_fileSystem->getDirectoryRead(DirectoryList::VAR_DIR)
      ->getAbsolutePath();

    $uploader->save($path . '/myfiles', $id . '.' . $uploader->getFileExtension());

    return '/myfiles' . $uploader->getUploadedFileName();
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.