for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace h4kuna\Upload;
use Nette\Http;
class Upload
{
/** @var DocumentRoot */
private $documentRoot;
public function __construct(DocumentRoot $documentRoot)
$this->documentRoot = $documentRoot;
}
/**
* Output path save to databese.
* @param Http\FileUpload $fileUpload
* @param string $path
* @param string|NULL $destinationAlias
* @throws FileUploadFaildException
* @return string
*/
public function save(Http\FileUpload $fileUpload, $path = '', $destinationAlias = NULL)
if (!$fileUpload->isOk()) {
throw new FileUploadFaildException($fileUpload->getName(), $fileUpload->getError());
} elseif ($path) {
$path = trim($path, '\/') . DIRECTORY_SEPARATOR;
do {
$relativePath = $path . $this->createName($fileUpload);
$pathname = $this->documentRoot->createAbsolutePath($relativePath, $destinationAlias);
} while (is_file($pathname));
$fileUpload->move($pathname);
return $relativePath;
* Prepare file name for save to file system.
protected function createName(Http\FileUpload $fileUpload)
$ext = pathinfo($fileUpload->getName(), PATHINFO_EXTENSION);
return sha1(microtime(TRUE) . '.' . $fileUpload->getName()) . '.' . $ext;