PHP code example of ijin82 / flysystem-azure

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

    

ijin82 / flysystem-azure example snippets


# Upload example
Storage::disk('disk1')->put('file-folder/file1.png',   
  file_get_contents('/my/file/path/file1.png'),  
  [  
    'mimetype' => 'image/png',  
  ]  
);  

# Get file URL example
$publicUrl = Storage::disk('disk1')->url('file-folder/file1.png');

# Check file exists example
$exists1 = Storage::disk('disk1')->exists('file-folder/file1.png');

# Get file contents example
$contents = Storage::disk('disk1')->get('file-folder/file1.png');

# Delete file example
Storage::disk('disk1')->delete('file-folder/file1.png');

# Delete directory example
# Warning, recursive folder deletion!
Storage::disk('disk1')->deleteDir('file-folder');

# Put uploaded file to storage example
# $file could be file path on disk (string) OR type of File|UploadedFile 
Storage::disk('disk1')->putFileAs('file-folder', $file, 'file1.png'); 

public function someUploadFuncName(Request $request)
{
    $file = $request->file('file_name_from_request');  
    
    // .. file name logic
    // .. file folder logic
    
    $file->storeAs($fileFolder, $fileName, [
        'disk' => 'my_azure_disk1'
    ]);  
    
    // save file name logic
    // to create file URL by name later
    // maybe you want to save file name and folder separated
    $fileNameToSave = $folderName . '/' . $diskFileName;
    
    // .. save file name to DB or etc.
}

echo Storage::disk('my_azure_disk1')->url($fileName);

public function someDeleteFuncName($id)
{
    $file = SomeFileModel::findOrFail($id);
    Storage::disk('my_azure_disk1')->delete($file->name);
    $file->delete();

    // go back or etc..
}

$fileConents = Storage::disk('public_or_another_local_disk')->get($file);

$forcedMimes = [
    'js' => 'application/javascript',
    'json' => 'application/json',
];

$fileExt = \File::extension($file);

if (array_key_exists($fileExt, $forcedMimes)) {
    $fileMime = $forcedMimes[$fileExt];
} else {
    $fileMime = mime_content_type(Storage::disk('public_or_another_local_disk')->path($file));
}

Storage::disk('my_custom_azure_disk')->put($fileName, $fileConents, [
    'mimetype' => $fileMime,
]);