Skip to content

Instantly share code, notes, and snippets.

@oliverlundquist
Last active October 21, 2015 06:09
Show Gist options
  • Save oliverlundquist/538521859992c3872906 to your computer and use it in GitHub Desktop.
Save oliverlundquist/538521859992c3872906 to your computer and use it in GitHub Desktop.
<?php
class SymfonyFile extends SplFileInfo
{
public function __construct($path, $originalName, $mimeType = null, $size = null) {
parent::__construct($path);
}
}
class FileHandler
{
// Needed to create a class-wide pointer to the temporary file, otherwise it'll be GC'ed
protected $filePointer = null;
// NB: you can get localpath this way
public function localpath() {
if ($this->file->isFile()) {
return (string)$this->file;
}
return sys_get_temp_dir() . '/' . $this->getHash();
}
/**
* Destruct the class and unlink the tempfile.
*
* @return void
*/
/**
* @codeCoverageIgnore
*/
public function __destruct()
{
if ($this->delete && $this->file instanceof SymfonyFile && getenv('APP_ENV') !== 'testing') {
unlink($this->localpath());
}
}
/**
* Set the file instance manually.
*
* @param string $path
* @param string $originalName
* @param string $mimeType
* @param int $size
* @return void
*/
public function setFile($path, $originalName, $mimeType, $size)
{
$this->file = new SymfonyFile($path, $originalName, $mimeType, $size);
}
/**
* Create and set the file instance manually.
*
* @param string $filename
* @param string $mimeType
* @return bool
*/
public function createFile($filename, $mimeType = 'text/csv')
{
$validMimeTypes = ['text/csv', 'application/xml', 'image/jpeg', 'image/gif', 'image/png'];
// valid mime type?
if ( ! in_array($mimeType, $validMimeTypes)) {
return false;
}
// create file
$this->filePointer = tmpfile();
// get filepath
$path = stream_get_meta_data($this->filePointer)['uri'];
// set file
$this->setFile($path, $filename, $mimeType, $size = 0);
// success!
return true;
}
/**
* Read file.
*
* @return array
*/
public function readFile()
{
// 1. create data placeholder
$data = [];
// 2. open file
$file = $this->file->openFile('r+b');
// 3. read!
while ( ! $file->eof()) {
$data[] = $file->fgets();
}
// 4. close file pointer
$file = null;
// 5. return data as array
return $data;
}
/**
* Write to file.
*
* @param array $data
* @return void
*/
public function writeToFile($data)
{
// 1. open file
$file = $this->file->openFile('r+b');
// 2. write!
$file->fwrite(implode(PHP_EOL, $data));
// 3. get new size
$size = $file->fstat()['size'];
// 4. close file pointer
$file = null;
// 5. refresh SymfonyFile instance
$path = $this->localpath();
$filename = $this->file->getClientOriginalName();
$mimeType = $this->file->getClientMimeType();
// TODO: check that $this->file->getClientSize() is returning size = 0
$this->setFile($path, $filename, $mimeType, $size);
// TODO: check that $this->file->getClientSize() is returning size bigger than 0
}
}
$fileHandler = new FileHandler;
$filename = time() . '.csv';
$data = ['line1', 'line2', 'line3', 'line4', 'line5'];
$fileHandler->createFile($filename);
$fileHandler->writeToFile($data);
echo implode('', $fileHandler->readFile()) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment