Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active January 17, 2018 13:32
Show Gist options
  • Save afiqiqmal/3b5c9b3990a4261f797750af2e09a3b1 to your computer and use it in GitHub Desktop.
Save afiqiqmal/3b5c9b3990a4261f797750af2e09a3b1 to your computer and use it in GitHub Desktop.
Helper function for focusing on Laravel Storage. Easier to save and get files
<?php
if (! function_exists('store_file')) {
/**
* @param $file
* @param string $disk
* @param bool $add_time
* @param bool $replace_space
* @param null $prefix
* @return bool
*/
function store_file($file, $disk = "local", $add_time = true, $replace_space = false, $prefix = null)
{
$filePath = rename_file($file, $add_time, $replace_space, $prefix);
Storage::disk($disk)->put($filePath, File::get($file));
return $filePath;
}
}
if (! function_exists('get_store_file')) {
/**
* @param $filePath
* @param string $disk
* @param null $defaultFile
* @param bool $public_path
* @return string
*/
function get_store_file($filePath, $disk = "local", $defaultFile = null, $public_path = false)
{
if ($filePath) {
if (Storage::disk($disk)->has($filePath)) {
if ($public_path) {
return get_full_storage_path($filePath, $disk);
}
return Storage::disk($disk)->url($filePath);
}
}
if ($defaultFile == -1) {
return null;
}
if ($public_path) {
return public_path('img/default.png');
}
return ($defaultFile) ? $defaultFile : asset('img/default.png');
}
}
if (! function_exists('get_full_storage_path')) {
/**
* @param $filePath
* @param string $disk
* @return string
*/
function get_full_storage_path($filePath, $disk = "local")
{
return Storage::disk($disk)->getDriver()->getAdapter()->getPathPrefix().$filePath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment