File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Files/FileService.php
<?php
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace KommaApp\Files;
class FileService
{
private $path;
public function uploadFiles($files, $fileSizes = [], $type = 'file')
{
$filesData = [];
foreach ($files as $file) {
if (!$fileData = $this->uploadFile($file)) continue;
$filesData[] = $fileData;
}
return $filesData;
}
public function uploadFile($file)
{
//Make the dir if needed
$this->makeDir($this->path);
$fileName = $file->getClientOriginalName();
//Todo check if file exist(?)
$file->move($this->path, $fileName);
if(!file_exists($this->path.'/'.$fileName)) \App::abort(404, 'Fout met uploaden');
//Return the file info
return [
'file_name' => $fileName,
'full_path' => $this->path . '/' . $fileName,
'relative_path' => $this->relativePath($this->path . '/' . $fileName)
];
}
function __set($name, $value)
{
$this->$name = $value;
}
public function makeDir()
{
if (is_dir($this->path)) return;
mkdir($this->path, 0777, true);
}
public function relativePath($path){
// At first ltrim was used here, but this didn't work in production environment
$path = str_replace(storage_path(),'',$path);
$firstCharacter = substr($path,0,1);
if($firstCharacter == '/' || $firstCharacter == '\\') $path = substr($path,1);
return $path;
}
public function deleteFile($filePath){
//Delete the file
\File::delete($filePath);
//Check if it exists, if true, return false deleting went wrong probably file rights
if(\File::exists($filePath)) return false;
return true;
}
}