From 4a919df3d2a708473a2b3f9f46ab1ca1d917e355 Mon Sep 17 00:00:00 2001 From: Stefano Sala Date: Tue, 11 Mar 2014 11:30:18 +0100 Subject: [PATCH] Added fileCopy to File provider to simulate file upload --- src/Faker/Provider/File.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Faker/Provider/File.php b/src/Faker/Provider/File.php index 671ccb2d..0bf20ec9 100644 --- a/src/Faker/Provider/File.php +++ b/src/Faker/Provider/File.php @@ -560,4 +560,31 @@ class File extends \Faker\Provider\Base return is_array($random_extension) ? static::randomElement($random_extension) : $random_extension; } + + /** + * Copy a random file from the source directory to the target directory and returns the filename/fullpath + * + * @param string $sourceDirectory The directory to look for random file taking + * @param string $targetDirectory + * @param boolean $fullPath Wether to have the full path or just the filename + * @return string + */ + public static function fileCopy($sourceDirectory, $targetDirectory, $fullPath = true) + { + // Drop . and .. and reset keys + $files = array_values(array_diff(scandir($sourceDirectory), array('.', '..'))); + + $sourceFullPath = $sourceDirectory . DIRECTORY_SEPARATOR . static::randomElement($files); + + $destinationFile = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)) . '.' . pathinfo($sourceFullPath, PATHINFO_EXTENSION); + $destinationFullPath = $targetDirectory . DIRECTORY_SEPARATOR . $destinationFile; + + copy($sourceFullPath, $destinationFullPath); + + if (!file_exists($destinationFullPath)) { + return false; + } + + return $fullPath ? $destinationFullPath : $destinationFile; + } }