HomeDocumentationTutorialsIncluding an upload in my module

Including an upload in my module

This tutorial will guide you through the file structure of a module within your framework and explain how to use specific methods for uploading files.

Sample Code Explanation

Below is an example code snippet for uploading files within the framework.

PHP
$objConfig->upload('picture', 'user-picture', 0);
$objConfig->max((1024 * 1024) / 1.5);
$objConfig->ajaxUpload(true);
$objConfig->filesystemStorage(KFW_STORAGE_PATH . 'user' . DIRECTORY_SEPARATOR);

Code Breakdown

Uploading a File

PHP
$objConfig->upload('picture', 'user-picture', 0);
  • upload('picture', 'user-picture', 0): This method initializes the upload process.
  • 'picture': The input field name in the HTML form matching the field name in your database.
  • 'user-picture': The language tag for tehn upload field
  • 0: Indicates the if the upload is requied or not

Setting the Maximum File Size

PHP
$objConfig->max((1024 * 1024) / 1.5);
  • max((1024 * 1024) / 1.5): Sets the maximum file size for the upload.
  • (1024 * 1024) / 1.5: Converts the size to bytes. In this case, it sets the maximum size to approximately 683 KB.

Enabling AJAX Upload

PHP
$objConfig->ajaxUpload(true);

ajaxUpload(true): Enables AJAX upload functionality. This allows files to be uploaded asynchronously without refreshing the page.

Setting Filesystem Storage Path

PHP
$objConfig->filesystemStorage(KFW_STORAGE_PATH . 'user' . DIRECTORY_SEPARATOR);
  • filesystemStorage(KFW_STORAGE_PATH . 'user' . DIRECTORY_SEPARATOR): Sets the storage path for the uploaded files
  • KFW_STORAGE_PATH . 'user' . DIRECTORY_SEPARATOR: Combines the base storage path with the user directory to ensure files are stored in the correct location.

Note: It is recommended to utilize $this->getComponentRealname() when referencing the component name. This practice ensures that if the component name changes in the future, you will not need to manually update your code. The optimal example of this approach is:

PHP
$objConfig->filesystemStorage(KFW_STORAGE_PATH . $this->getComponentRealname(). DIRECTORY_SEPARATOR);

Conclusion

This tutorial provided an overview of the file structure for a module in your framework and explained a sample code snippet for uploading files. Following this structure and understanding the provided methods will help you maintain and scale your application efficiently.

Feel free to expand upon this tutorial by adding more specific details related to your framework's features and functionalities.

There's more functions to customize your upload:

  • fileMaxHeight()
  • fileMaxWidth()
  • fileMinHeight()
  • fileMinWidth()
  • fileSquare()
  • ftpStorage()
  • encryptUploadedFile()
  • ignoreUploadErrors()
  • sourceComponent()
  • sourceModule()