mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-25 17:59:47 +01:00
Merge pull request #279 from stefanosala/feature/fileCopy
Added fileCopy to File provider to simulate file upload
This commit is contained in:
commit
89345a3ccc
@ -199,6 +199,9 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
|
||||
|
||||
fileExtension // 'avi'
|
||||
mimeType // 'video/x-msvideo'
|
||||
// Copy a random file from the source to the target directory and returns the fullpath or filename
|
||||
file($sourceDir = '/tmp', $targetDir = '/tmp') // '/path/to/targetDir/13b73edae8443990be1aa8f1a483bc27.jpg'
|
||||
file($sourceDir, $targetDir, false) // '13b73edae8443990be1aa8f1a483bc27.jpg'
|
||||
|
||||
### `Faker\Provider\Image`
|
||||
|
||||
|
@ -560,4 +560,37 @@ 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 file($sourceDirectory = '/tmp', $targetDirectory = '/tmp', $fullPath = true)
|
||||
{
|
||||
if (!is_dir($sourceDirectory)) {
|
||||
throw new \InvalidArgumentException(sprintf('Source directory %s does not exist or is not a directory.', $sourceDirectory));
|
||||
}
|
||||
|
||||
if (!is_dir($targetDirectory)) {
|
||||
throw new \InvalidArgumentException(sprintf('Target directory %s does not exist or is not a directory.', $targetDirectory));
|
||||
}
|
||||
|
||||
// Drop . and .. and reset array keys
|
||||
$files = array_values(array_diff(scandir($sourceDirectory), array('.', '..')));
|
||||
|
||||
$sourceFullPath = $sourceDirectory . DIRECTORY_SEPARATOR . static::randomElement($files);
|
||||
|
||||
$destinationFile = Uuid::uuid() . '.' . pathinfo($sourceFullPath, PATHINFO_EXTENSION);
|
||||
$destinationFullPath = $targetDirectory . DIRECTORY_SEPARATOR . $destinationFile;
|
||||
|
||||
if (false === copy($sourceFullPath, $destinationFullPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $fullPath ? $destinationFullPath : $destinationFile;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user