Sunday, November 20, 2016

Filesystem in laravel

Hi

Today we will learn, how to store file in laravel.

Laravel framework comes with built in storage handling which is compatible with a variety of systems. It is built upon the popular library Flysystem and therefore integrates with lots of services via the same common interface.

Storage configuration resides in the “config/filesystems.php” file. We can add multiple engines such as local drives and cloud drives.

Basic file operations

The filesystem integration provides a straight forward way of interacting with files. Basic write/read operations are illustrated below.

 // Write content to file  
 $content = 'File content ...';  
 Storage::put( 'my_file.txt', $content );  
 // Read content from file  
 $content = Storage::get( 'my_file.txt' );  
 // Delete file  
 Storage::delete( 'my_file.txt' );  
 You can also append/prepend content easily to existing files.  
 // Append content to file  
 $append = 'Appended text...';  
 Storage::append( 'my_file.txt', $append );  
 // Prepend content to file  
 $prepend = 'Prepended text...';  
 Storage::prepend( 'my_file.txt', $prepend );  

Listing files and directories

Sometimes we need to read your file structure which can be done with the listing methods.
We can list both files and directories in one level or recursively.

 // List all files in directory  
 $files = Storage::files($directory);  
 // List all files & directories in directory  
 $files = Storage::allFiles($directory);  
 // List all directories in directory  
 $directories = Storage::directories($directory);  
 // List all directories recursively in directory  
 $directories = Storage::allDirectories($directory);  


Thanks

No comments:

Post a Comment