Ness PHP File Uploads
Required Namespace
  • Ness
    • Forms
      • FileUpload

You can include this library to your code file by typing the below code;

use Ness\Forms\FileUpload


File Upload Class

Uploading files with Ness PHP framework is extremely easy. You can upload your files with a single line.


UploadFile()

This function is the main function of class, used to upload the file


$var_name->UploadFile(string  $file_uploader_form_element_name) : bool
                    

$file_uploader_form_element_name: [IMPORTANT] Please enter the 'name' of attribute on your view / fileupload control.


OnlyImages()

This is a security function, you can set it true accept only image formats.


$var_name->OnlyImages(TRUE | FALSE) : $this
                    



MaxFileSize()

This is a security function, you can set a max value for file size in bytes. By default alllowd upload size is: 50000 bytes


$var_name->MaxFileSize(int $MAX_ALLOWED_FILESIZE) : $this
                    



setDirectory()

This function is used to set the folder name for your uploads. Your folder must be placed in 'application' folder.


$var_name->setDirectory(string  $folder_Name) : $this
                    



getDirectory()

This function is used to return the folder path.


$var_name->getDirectory() :  String
                    



getName()

This function is used to return the uploaded file name. This is useful when you need to store the name in database.


$var_name->getName() :  String
                    



AllowedExtensions()

This function is used to set allowed extension for your file uploads.


$var_name->AllowedExtensions(Array  $extension_list) :  $this
                    



getErrors()

This function is used to return errors occurred in file upload


$var_name->getError() :  Array
                    





Warning If a file is already exists in your upload folder, Ness PHP framework will add it a timestamp and upload with new file name. Overwrite is not allowed.

Example

First of all we need to create a view for our form, please add new item in application/view folder. uploadView.php


                    
PHP:
use Ness\Forms\Form;
use Ness\Forms\FormElements;

$fileUpload = new Form("test");



    .
    ..
    ...
    ....
    echo $fileUpload->DefineForm(array("method"=>"post", "enctype"=>"multipart/form-data"));
    $fileUpload->setModel("testModel");
    
echo $fileUpload->setElement(FormElements::Label("File", "mydata"));
echo $fileUpload->setElement(FormElements::FileUpload("mydata"));

echo $fileUpload->setElement(FormElements::Submit("test"));
    $fileUpload->Call("upload");

                    

In theese code lines we have defined that; we have a testModel.php file in Model folder and a upload() function in it. Now, we need to create the Model/testModel.php and code it to look like this:

PHP:



use Ness\Forms\FileUpload;

class testModel
{

public function upload()
{
    $tryupload = new FileUpload("test");
    
    if($tryupload->setDirectory("uploads")->OnlyImages(true)->UploadFile("mydata"))
    {
        echo "FILE UPLOADED SUCCESSFULLY";
    }else{
        var_dump($tryupload->getErrors());
    }

}
}

                    
thats all.