Advanced File Operations

Andy Snell | Dallas PHP | March 10, 2020

@andrewsnell

What Are File Operations?

@andrewsnell
  • Browsing A Filesystem
  • Getting File Metadata
  • Reading File Contents
  • Writing a New File
  • Appending an Existing File
  • Deleting an Existing File

What Are File Operations?

@andrewsnell

Filesystem CRUD / BREAD

But First, A cavaet...

@andrewsnell

"Advanced" !== "Better"

Sometimes Basic is Best!

echo file_get_contents("lorem.txt");

"Procedural" !== "Wrong"

What Are "Advanced" File Operations?

@andrewsnell

 

Approaches to file operations that support abstraction and scalability

 

What Are "Advanced" File Operations?

@andrewsnell

Advanced Operations

may help you interact with a filesystem in a more efficient or maintainable way

 

Key Concepts

@andrewsnell
  • Files as Objects
  • Files and Filesystems as Iterators
  • Streams: Files in the Abstract
  • Using the Flysystem Library

Files & PHP

@andrewsnell

The "Standard" PHP File Operations

@andrewsnell

Entirely Procedural

~81 built-in core file operation functions

and only two operative data-types:

(string)
(resource)

The "Standard" PHP File Operations

@andrewsnell

What's a "Resource"?

A data type for holding a reference

to an external resource

Not File Specific!

106 Different Types of Resources

Run Example #0
@andrewsnell

The "Standard" PHP File Operations

@andrewsnell

Often More Than One (Right?)

To Do One Kind of Operation

fread
readfile
fgetc
fgetcsv
fscanf
file_get_contentsfgets
fgetss
fpassthru
file

How many (core) ways to read a file?

Run Example #1
@andrewsnell

The "Standard" PHP File Operations

@andrewsnell

Frequently Multiple Steps Needed

Writing to a File and Reading It Back

  1. Open the File (fopen)
  2. Write to the File (fwrite)
  3. Rewind the File (rewind)
  4. Read the File (fread)
  5. Close the File (fclose)
Run Example #2
@andrewsnell

File As Objects

@andrewsnell

Enter the SPL

@andrewsnell

The "Standard PHP Library" is an extension to PHP that defines a number of interfaces and objects meant to solve common problems.* 

* At the time it was added to the language in 2004

Enter the SPL

@andrewsnell

File Metadata as Object

SplFileInfo
(Show Example #3)

Enter the SPL

@andrewsnell

File As Iterable Object

SplFileObject
(Show Examples #4, #5, #6)

Enter the SPL

@andrewsnell

File As Temporary Iterable Object

SplTempFileObject

Enter the SPL

@andrewsnell

Iterable Directory Structure

FilesystemIterator
(Show Example #7)

Streams Vs. Strings

@andrewsnell

Strings Vs. Streams

@andrewsnell
Fatal error: Allowed memory size of 8388608 bytes exhausted…

Strings Vs. Streams

@andrewsnell

Strings are easy to work with, but are memory bound.

 Streams are the way of generalizing file, network, and other operations which share a common set of functions and uses. In its simplest definition, A stream is a resource object which can be read from or written to in a linear fashion, and may be able to seek to an arbitrary location within the stream.

A stream is a resource object which can be read or written in a linear fashion, and may be able to seek to an arbitrary location within the stream.

Strings are easy to work with, but are memory bound.

Flysystem

@andrewsnell

Flysystem: A Filesystem Abstraction

@andrewsnell

The "filesystem" as an object

$filesystem = new Filesystem($adapter);

// write files
$response = $filesystem->write($path, $contents);
$response = $filesystem->writeStream($path, $resource [, $config]);

// read files
$contents = $filesystem->read($path);
$resource = $filesystem->readStream($path);

// do other file things
$contents = $filesystem->listContents($path, $recursive);
$file_exists = $filesystem->has($path);
$filesystem->delete($path);
$filesystem->rename($from, $to);

Flysystem: A Filesystem Abstraction

@andrewsnell
  • Local Filesystem
  • Local Memory
  • .zip Files
  • FTP / SFTP
  • AWS S3
  • Azure File Storage
  • Azure Blob Storage
  • Google Drive
  • Google Cloud Storage
  • Dropbox
  • chroot
  • Digital Ocean Spaces
  • Rackspace
  • Database via PDO
  • Remote via SSH
  • Redis
  • PHPCR​
  • WebDAV
  • OneDrive
  • GitHub

One Interface

Many "Filesystems"

Flysystem: Other Benefits

@andrewsnell

Metadata & Directory Caching

$aws_adapter = new League\Flysystem\AwsS3V3\AwsS3Adapter($aws_client, $muh_bucket);

$memory_cache = new League\Flysystem\Cached\Storage\Memory();

$redis_cache = new League\Flysystem\Cached\Storage\PhpRedis($redis_client);

$adapter = new League\Flysystem\Cached\CachedAdapter($aws_adapter, $memory_cache);
$adapter = new League\Flysystem\Cached\CachedAdapter($aws_adapter, $redis_cache);

$filesystem = new Filesystem($adapter);

Flysystem: Other Benefits

@andrewsnell

Replication Across Filesystems

Write to Local and Cloud / Read from Local Only

$source = new League\Flysystem\Adapter\Local($local_path);

$replica = new League\Flysystem\AwsS3V3\AwsS3Adapter($aws_client, $muh_bucket);

$adapter = new League\Flysystem\Replicate\ReplicateAdapter($source, $replica);

$filesystem = new Filesystem($adapter);

Flysystem 2.0

@andrewsnell

Complete Re-Write of Library

Fix Method Names to Be Predictable and Readable

Less Arrays, More ArrayAccess

Uses Generators for Directory Listings, Not Arrays

Questions?

@andrewsnell
Andy Snell

Advanced File Operations

By Andy Snell

Advanced File Operations

  • 95