PHP code example of samsonphp / upload

1. Go to this page and download the library: Download samsonphp/upload library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samsonphp / upload example snippets


class UploadConfig extends \samson\core\Config
{
    // Callback function for creating upload dir
    public $uploadDirHandler = 'uploadDirHandler';

    // Callback function for creating file name
    public $fileNameHandler = 'uploadNameHandler';
}

function catalog_async_upload()
{
    // Parameter for callback functions
    $parameter = 5;

    // Create AJAX response array
    $json = array('status' => 0);

    // Create object for uploading file to server
    $upload = new \samsonphp\upload\Upload(array('png','jpg'), $parameter);

    if ($upload->upload($filePath, $fileName, $realName) {
        $json['status'] = 1;
        // Add full path to uploaded file into AJAX response
        $json['filePath'] = $filePath;
        // Add uploaded file name into AJAX response
        $json['fileName'] = $fileName;
        // Add real file name (user's file name) into AJAX response
        $json['realName'] = $realName;
    }

    return $json;
}

/**
* External handler for defining upload catalog
*/
function uploadDirHandler($parameter)
{
    return 'upload/catalog'.$parameter;
}

/**
* External handler for defining upload file name
*/
function uploadNameHandler($parameter, $extension)
{
    return 'item'.$parameter.$extension;
}
js
// Cache file input
var file = s('.__example_upload');

// Bind upload event
uploadFileHandler(file, {
    // Handle event before file uploading
    start: function () {
        alert('Uploading started');
    },
    // Handle event after upload finishing
    response: function (response) {
        alert('Uploading finished');
    }
});
$extension