PHP code example of popphp / pop-storage

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

    

popphp / pop-storage example snippets


// 2.x
$size = $storage->getFileSize('test.pdf');
if ($size === false) {
    // not found
}

// 3.0
try {
    $size = $storage->getFileSize('test.pdf');
} catch (Pop\Storage\Exception\FileNotFoundException $e) {
    // not found
}

use Pop\Storage\Storage;

$storage = Storage::createAzure('ACCOUNT_NAME', 'ACCOUNT_KEY', 'CONTAINER');

$storage->putFile('test.pdf');

$fileContents = $storage->fetchFile('test.pdf');

use Pop\Storage\Storage;

$storage = Storage::createS3('AWS_BUCKET', new S3\S3Client([
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET',
    ],
    'region'  => 'AWS_REGION',
    'version' => 'AWS_VERSION'
]));

use Pop\Storage\Storage;

$storage = Storage::createAzure('ACCOUNT_NAME', 'ACCOUNT_KEY', 'CONTAINER');

use Pop\Storage\Storage;

$storage = Storage::createLocal(__DIR__ . '/tmp/');

try {
    $contents = $storage->fetchFile('test.pdf');
} catch (Pop\Storage\Exception\FileNotFoundException $e) {
    // Handle the missing file specifically
} catch (Pop\Storage\Exception $e) {
    // Or catch any storage failure
}

$storage->putFile('test.pdf');

$storage->putFileContents('test.pdf', $fileContents);

$storage->replaceFileContents('test.pdf', $newFileContents);

$fileContents = $storage->fetchFile('test.pdf');

// Returns an array of file info:
$info = $storage->fetchFileInfo('test.pdf');

$resource = fopen('/path/to/large-video.mp4', 'r');
$storage->putFileStream('large-video.mp4', $resource);
fclose($resource);

$resource = $storage->fetchFileStream('large-video.mp4');
while (!feof($resource)) {
    echo fread($resource, 8192);
}
fclose($resource);

// Valid for 15 minutes (900 seconds) by default
$url = $storage->getTemporaryUrl('test.pdf');

// Or, set the expiration explicitly
$url = $storage->getTemporaryUrl('test.pdf', 3600);

$storage->uploadFiles($_FILES);

// Where $file follows the $_FILES array format specified in PHP:
// $file = ['tmp_name' => '/tmp/Hs87jdk', 'name' => 'test.pdf', 'size' => 8574, 'error' => 0]
$storage->uploadFile($file);

$files = $storage->listFiles();

$files = $storage->listFiles('test*');

$files = $storage->listFiles('*.pdf');

$all = $storage->listAll();

// ['test.pdf', 'foo/test2.pdf', 'foo/bar/test3.pdf']
$files = $storage->listFiles(null, true);

$dirs = $storage->listDirs(null, true);
$all  = $storage->listAll(null, true);

foreach ($storage->listFiles(null, true) as $file) {
    $contents = $storage->fetchFile($file);
}

// The source file remains
$storage->copyFile('test.pdf', 'foo/test2.pdf');

// The source file no longer exists
$storage->renameFile('test.pdf', 'foo/test2.pdf');

// AWS example. The source file remains
$storage->copyFileToExternal('test.pdf', 's3://other-bucket/test.pdf');

// Azure example. The source file remains
$storage->copyFileToExternal('test.pdf', '/other-container/test.pdf');

// AWS example. The source file no longer exists
$storage->moveFileToExternal('test.pdf', 's3://other-bucket/test.pdf');

// Azure example. The source file no longer exists
$storage->moveFileToExternal('test.pdf', '/other-container/test.pdf');

// AWS example. The source file remains
$storage->copyFileFromExternal('s3://other-bucket/test.pdf', 'test.pdf');

// Azure example. The source file remains
$storage->copyFileFromExternal('/other-container/test.pdf', 'test.pdf');

// AWS example. The source file no longer exists
$storage->moveFileFromExternal('s3://other-bucket/test.pdf', 'test.pdf');

// Azure example. The source file no longer exists
$storage->moveFileFromExternal('/other-container/test.pdf', 'test.pdf');

$storage->deleteFile('test.pdf');

$storage = Storage::createS3('s3://my-bucket', new S3\S3Client([
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET',
    ],
    'region'  => 'AWS_REGION',
    'version' => 'AWS_VERSION'
]));

// Create the bucket 's3://my-bucket/foo'
$storage->mkdir('foo');

// Point the adapter at 's3://my-bucket/foo'
// Any files pushed will store here
// Any delete calls will delete files from here
$storage->chdir('foo');

// Removes the bucket and its content
$storage->rmdir('foo');

$storage->setBaseDir('s3://my-bucket');
$storage->getBaseDir();    // 's3://my-bucket'
$storage->chdir('foo');
$storage->getCurrentDir(); // 's3://my-bucket/foo'

$dirs = $storage->listDirs();

$dirs = $storage->listDirs('foo*');

$dirs = $storage->listDirs('*foo/');

$all = $storage->listAll();

$dirs = $storage->listDirs(null, true);

var_dump($storage->fileExists('test.pdf'))    // Returns bool
var_dump($storage->isDir('foo'));             // Returns bool
var_dump($storage->isFile('test.pdf'));       // Returns bool
var_dump($storage->getFileSize('test.pdf'));  // Returns filesize value as an integer
var_dump($storage->getFileType('test.pdf'));  // Return either 'file' or 'dir'
var_dump($storage->getFileMTime('test.pdf')); // Returns date/time value
var_dump($storage->md5File('test.pdf'));      // Returns MD5 hash of file

$adapter = $storage->adapter(); // same as $storage->getAdapter()

// fetchFile()'s second argument: pass false to get the raw Pop\Http\Client\Response back
// instead of the file contents as a string (useful for inspecting response headers).
$response = $storage->adapter()->fetchFile('test.pdf', false);

// deleteFile() and moveFileFromExternal() take an optional trailing $snapshots argument
// controlling Azure's x-ms-delete-snapshots header when the blob has snapshots:
// '