本文整理汇总了PHP中Symfony\Component\HttpFoundation\File\File::guessClientExtension方法的典型用法代码示例。如果您正苦于以下问题:PHP File::guessClientExtension方法的具体用法?PHP File::guessClientExtension怎么用?PHP File::guessClientExtension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\File\File
的用法示例。
在下文中一共展示了File::guessClientExtension方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: upload
/**
* $current_uri String actual uri of the file
* $dest_folder String future uri of the file starting from web/upload folder
* $lifetime DateTime lifetime of the file. If time goes over this limit, the file will be deleted.
**/
public function upload(File $file, $dest_folder = '', \DateTime $lifetime = null)
{
if ($file instanceof UploadedFile) {
if ($file->getError() !== null && $file->getError() !== 0) {
throw new UploadException($file->getErrorMessage());
}
}
//preparing dir name
$dest_folder = date('Ymd') . '/' . date('G') . '/' . $dest_folder;
//checking mimetypes
$mimeTypePassed = false;
foreach ($this->allowedMimetypes as $mimeType) {
if (preg_match('@' . $mimeType . '@', $file->getMimeType())) {
$mimeTypePassed = true;
}
}
if (!$mimeTypePassed) {
throw new InvalidMimeTypeException('Only following filetypes are allowed : ' . implode(', ', $this->allowedMimetypes));
}
$fs = new Filesystem();
if (!$fs->exists($this->uploadDir . $dest_folder)) {
$fs->mkdir($this->uploadDir . $dest_folder);
}
$em = $this->entityManager;
$media = new Media();
$media->setMime($file->getMimeType());
// If there's one, we try to generate a new name
$extension = $file->getExtension();
// Sanitizing the filename
$slugify = new Slugify();
if ($file instanceof UploadedFile) {
if (empty($extension)) {
$extension = $file->getClientOriginalExtension();
if (empty($extension)) {
$extension = $file->guessClientExtension();
}
}
$filename = $slugify->slugify(basename($file->getClientOriginalName(), $extension)) . '.' . $extension;
} else {
if (empty($extension)) {
$extension = $file->guessClientExtension();
}
$filename = $slugify->slugify(basename($file->getFilename(), $extension)) . '.' . $extension;
}
// A media can have a lifetime and will be deleted with the cleanup function
if (!empty($lifetime)) {
$media->setLifetime($lifetime);
}
// Checking for a media with the same name
$mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder . $filename);
$mediaExists = count($mediaExists) > 0;
if ($mediaExists === false) {
$mediaExists = $fs->exists($this->uploadDir . $dest_folder . $filename);
}
if ($mediaExists === true) {
$filename = basename($filename, '.' . $extension);
$i = 1;
do {
$media->setName($filename . '-' . $i++ . '.' . $extension);
$media->setUri($dest_folder . $media->getName());
$mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri());
$mediaExists = count($mediaExists) > 0;
if ($mediaExists === false) {
$mediaExists = $fs->exists($this->uploadDir . $dest_folder . $filename);
}
} while ($mediaExists === true);
} else {
$media->setName($filename);
$media->setUri($dest_folder . $media->getName());
}
$file->move($this->uploadDir . $dest_folder, $media->getName());
chmod($this->uploadDir . $dest_folder . $media->getName(), 0664);
// Getting the salt defined in parameters.yml
$secret = $this->container->getParameter('secret');
$media->setSecretKey(hash('sha256', $secret . $media->getName() . $media->getUri()));
$em->persist($media);
$em->flush();
return $media;
}