From bcbac4312ac0b609a47517c12625b3a5c290863c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Wed, 28 Jan 2015 15:23:52 +0100 Subject: [PATCH] =?UTF-8?q?UploadedFile:=20ajout=20de=20la=20m=C3=A9thode?= =?UTF-8?q?=20getLocalPath.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Entity/UploadedFile.php | 36 +++++++++++++++++++++++++++++++++ Model/UploadedFileInterface.php | 7 +++++++ 2 files changed, 43 insertions(+) diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index e4926bcc..e59246b7 100644 --- a/Entity/UploadedFile.php +++ b/Entity/UploadedFile.php @@ -91,6 +91,12 @@ class UploadedFile implements UploadedFileInterface */ private $filesystem; + /** Contient le nom de chemin local. + * + * @var string + */ + private $localTempPath = null; + /** * */ @@ -581,4 +587,34 @@ class UploadedFile implements UploadedFileInterface 'icon' => MimeTypeIcon::getMimeTypeIcon($this->getMimeType()) ]; } + private $localTempPath = null; + + public function getLocalPath() + { + if(null !== $this->localTempPath) { + return $this->localTempPath; + } + + $stream = $this->filesystem->createStream($this->getPath()); + $stream->open(new StreamMode('rb')); + $handle = $stream->cast(STREAM_CAST_AS_STREAM); + + if(false !== $handle) { + if(stream_is_local($handle)) { + $this->localTempPath = stream_get_meta_data($handle)['uri']; + fclose($handle); + return $this->localTempPath; + } + fclose($handle); + } + + $this->localTempPath = tempnam(sys_get_temp_dir(), 'UploadedFile'); + register_shutdown_function('unlink', $this->localTempPath); + + $tmp = fopen($this->localTempPath, 'xb'); + $this->copyTo($tmp); + fclose($tmp); + + return $this->localTempPath; + } } diff --git a/Model/UploadedFileInterface.php b/Model/UploadedFileInterface.php index 567eb6db..7b46d603 100644 --- a/Model/UploadedFileInterface.php +++ b/Model/UploadedFileInterface.php @@ -217,6 +217,13 @@ interface UploadedFileInterface */ public function copyTo($dest, $maxlen = PHP_INT_MAX, $readOffset = 0); + /** Retourne un nom de fichier local pour ce fichier. + * + * Ce peut-être un fichier temporaire qui sera supprimé à la fin de la requête ! + * + * @return string + */ + public function getLocalPath(); /** * @return array -- GitLab