本文整理汇总了PHP中Uuid::uuid方法的典型用法代码示例。如果您正苦于以下问题:PHP Uuid::uuid方法的具体用法?PHP Uuid::uuid怎么用?PHP Uuid::uuid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Uuid
的用法示例。
在下文中一共展示了Uuid::uuid方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: file
/**
* 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
* Whether 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));
}
if ($sourceDirectory == $targetDirectory) {
throw new \InvalidArgumentException('Source and target directories must differ.');
}
// Drop . and .. and reset array keys
$files = array_filter(array_values(array_diff(scandir($sourceDirectory), array('.', '..'))), function ($file) use($sourceDirectory) {
return is_file($sourceDirectory . DIRECTORY_SEPARATOR . $file) && is_readable($sourceDirectory . DIRECTORY_SEPARATOR . $file);
});
if (empty($files)) {
throw new \InvalidArgumentException(sprintf('Source directory %s is empty.', $sourceDirectory));
}
$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;
}