Added Files Trait, added createParentDirs
This commit is contained in:
62
app/Traits/Files.php
Normal file
62
app/Traits/Files.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Log;
|
||||
use App\Model\Photo;
|
||||
|
||||
trait Files
|
||||
{
|
||||
/**
|
||||
* Get a list of files
|
||||
*/
|
||||
public function getFiles(array $option)
|
||||
{
|
||||
// Make sure we got a directory or a file to import
|
||||
if (is_null($option['file']) AND is_null($option['dir']))
|
||||
abort(500,'Missing filename, please use --file= OR --dir=');
|
||||
|
||||
Log::info(sprintf('%s: Processing: %s',__METHOD__,($option['file'] ? $option['file'] : $option['dir'])));
|
||||
|
||||
$files = [];
|
||||
|
||||
if ($option['dir'])
|
||||
{
|
||||
// Remove our trailing slash from the directory.
|
||||
$dir = preg_replace('/\/$/','',$option['dir']);
|
||||
|
||||
// Exclude . & .. from the path.
|
||||
$files = array_diff(scandir($dir),array('.','..'));
|
||||
|
||||
}
|
||||
elseif ($option['file'])
|
||||
{
|
||||
$dir = dirname($option['file']);
|
||||
$files = array(basename($option['file']));
|
||||
}
|
||||
|
||||
// Determine if our dir is releative to where we store data
|
||||
$dir = Photo::path($dir);
|
||||
|
||||
// Add our path
|
||||
if ($dir)
|
||||
array_walk($files,function(&$value,$key,$path='') {
|
||||
if ($path) {
|
||||
$value = sprintf('%s/%s',$path,$value);
|
||||
}
|
||||
},$dir);
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
public function makeParentDir($dir)
|
||||
{
|
||||
if (is_dir($dir))
|
||||
return TRUE;
|
||||
|
||||
if (! file_exists(dirname($dir)) AND ! $this->makeParentDir(dirname($dir)))
|
||||
return FALSE;
|
||||
else
|
||||
return is_writable(dirname($dir)) AND mkdir($dir);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user