PHP code example of floppy / client

1. Go to this page and download the library: Download floppy/client 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/ */

    

floppy / client example snippets



    use Floppy\Client\Factory;
    use Floppy\Common\FileSource;
    use Floppy\Common\FileId;

    //configure FloppyClient library
    $factory = new Factory(array(
        'host' => 'your-floppy-server-host',
        'secretKey' => 'your-floppy-secret-key-the-same-as-in-server',
    ));

    //create client and url generator
    $client = $factory->createFloppyClient();
    $urlGenerator = $factory->createUrlGenerator();

    //upload file
    $fileId = $client->upload(FileSource::fromFile($someSplFileInstance)); //$fileId is Floppy\Common\FileId instance

    //info about file type, file size, file mime type etc.
    $info = $fileId->info();
    $info->get('size');

    //value that you should store to be able to recreate FileId instance
    $someFileStringId = $fileId->id();
    $recreatedFileId = new FileId($someFileStringId);

    //generate url to image thumbnail
    $url = $urlGenerator->generate(
        //create identifier to concrete thumbnail
        $fileId->with([ 'thumbnail' => [ 'size' => [ 80, 80 ] ] ]) //assume uploaded file is an image
    );

    //generate url to original file
    $url = $urlGenerator->generate($fileId);


    $urlGenerator->generate($fileId, 'image', /** credential attrs */ array('expiration' => time() + 60, 'customFiled' => 'value'));
    $client->upload($fileSource, array('file_types' => 'image'));
    


    use Floppy\Client\Factory;
    
    $factory = new Factory(array(
        'credentialsGenerator' => function($container){
            return new CustomGenerator($container['checksumChecker']);
        }
    ));
    
    //...



    use Floppy\Client\Factory;
    
    $factory = new Factory(array(
        'urlGenerator.image.extensions' => array('jpeg', 'png', 'jpg'),//gif files will not be threaten as images
    ));
    


    use Floppy\Client\Factory;

    $factory = new Factory(array(
        'host' => 'your-floppy-server-host',
        'secretKey' => 'your-floppy-secret-key-the-same-as-in-server',
        /** other options */
    ));



    $urlGenerator = $factory->createUrlGenerator();



    use Floppy\Common\FileId;

    $fileId = new FileId('id-of-the-file.png');
    $url = $urlGenerator->generate($fileId->with(array(
        'thumbnail' => array(
            'size' => array(60, 60),
        ),
    ), /** explicitly file type */ 'image');



    $url = $urlGenerator->generate($fileId->with([ "name" => "Some name" ]));



    $client = $factory->createFloppyClient();



    use Floppy\Common\FileSource;
    use Floppy\Common\Stream\LazyLoadedInputStream;
    use Floppy\Common\FileType;
    
    //by factory method - it is recommended
    $fileSource = FileSource::fromFile(new \SplFileInfo('some/path'));//you can pass instance of `UploadedFile` from Symfony too
    
    //by constructor
    $fileSource = new FileSource(new LazyLoadedInputStream('some/path'), new FileType('image/jpg', 'jpg'));