本文整理匯總了PHP中Symfony\Component\HttpFoundation\File\File::getClientOriginalExtension方法的典型用法代碼示例。如果您正苦於以下問題:PHP File::getClientOriginalExtension方法的具體用法?PHP File::getClientOriginalExtension怎麽用?PHP File::getClientOriginalExtension使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\Component\HttpFoundation\File\File
的用法示例。
在下文中一共展示了File::getClientOriginalExtension方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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;
}
示例2: getFileExtension
public static function getFileExtension(File $file)
{
return $file instanceof UploadedFile ? $file->getClientOriginalExtension() : $file->getExtension();
}
示例3: uploadFile
/**
* @param File $file
* @param User $user
*/
private function uploadFile(File $file, User $user)
{
$filename = 'emergya-' . $user->getNick() . '.' . $file->getClientOriginalExtension();
$file->move($this->getParameter('uploads_directory'), $filename);
$user->setAvatar($filename);
}
示例4: createImage
/**
* Given a single File, assuming is an image, create a new
* Image object containing all needed information.
*
* This method also persists and flush created entity
*
* @param File $file File where to get the image
*
* @return ImageInterface Image created
*
* @throws InvalidImageException File is not an image
*/
public function createImage(File $file)
{
$fileMime = $file->getMimeType();
if ('application/octet-stream' === $fileMime) {
$imageSizeData = getimagesize($file->getPathname());
$fileMime = $imageSizeData['mime'];
}
if (strpos($fileMime, 'image/') !== 0) {
throw new InvalidImageException();
}
$extension = $file->getExtension();
if (!$extension && $file instanceof UploadedFile) {
$extension = $file->getClientOriginalExtension();
}
/**
* @var ImageInterface $image
*/
$image = $this->imageFactory->create();
if (!isset($imageSizeData)) {
$imageSizeData = getimagesize($file->getPathname());
}
$name = $file->getFilename();
$image->setWidth($imageSizeData[0])->setHeight($imageSizeData[1])->setContentType($fileMime)->setSize($file->getSize())->setExtension($extension)->setName($name);
return $image;
}
示例5: extractDataFromFileInfo
/**
* @param FileInfo $info
*
* @return array
*/
protected function extractDataFromFileInfo(FileInfo $info)
{
$data = [];
if ($info instanceof UploadedFile) {
$data['original_name'] = $info->getClientOriginalName();
$data['original_extension'] = $info->getClientOriginalExtension();
}
return $data;
}
示例6: upload
/**
* Upload a file, storing it in the temporary
*
* @param File $file
* @param Media $media
*
* @throws Exception\MediaFileDeniedException
* @return Media
*/
public function upload(File $file, Media $media = null)
{
if (!$media instanceof Media) {
$media = new $this->class();
}
if ($file instanceof UploadedFile) {
$extension = $file->getClientOriginalExtension();
$oldName = $file->getClientOriginalName();
} else {
$extension = $file->getExtension();
$oldName = $file->getFilename();
}
$fileMime = $file->getMimeType();
if (array_key_exists($fileMime, $this->accept) && in_array(strtolower($extension), $this->accept[$fileMime])) {
$newFileName = null;
// if the media name is set, use it over a hashed one
if ($media->getName()) {
if ($media->getPath() != $file->getPathname()) {
$newFileName = $media->getName();
}
} else {
$newFileName = md5(time() . rand()) . "." . $extension;
}
if ($newFileName) {
$file = $file->move($this->storePath, $newFileName);
}
} else {
throw new MediaFileDeniedException($fileMime);
}
if ($file instanceof File) {
/** @var Media $media */
if (!$media->getTitle()) {
$media->setTitle($oldName);
}
$oldPath = pathinfo($oldName);
if (!$media->getNameOriginal()) {
$media->setNameOriginal($oldName);
}
if (!$media->getName()) {
$media->setName($file->getFilename());
}
if (!$media->getAlt()) {
$media->setAlt($oldPath['filename']);
}
if (!$media->getCredits()) {
$media->setCredits($this->user->getUsername());
}
$media->setSrc($this->storeDirectory . $file->getFilename());
$media->setPath($this->storePath . $file->getFilename());
$this->optimiser->optimise($media);
$media->setHash(sha1_file($file->getPathname()));
}
return $media;
}