本文整理汇总了PHP中Symfony\Component\HttpFoundation\File\UploadedFile::getSize方法的典型用法代码示例。如果您正苦于以下问题:PHP UploadedFile::getSize方法的具体用法?PHP UploadedFile::getSize怎么用?PHP UploadedFile::getSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\File\UploadedFile
的用法示例。
在下文中一共展示了UploadedFile::getSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: orientate
/**
* Orientate the image.
*
* @param UploadedFile $file
* @param $orientation
* @return UploadedFile
*/
protected function orientate(UploadedFile $file, $orientation)
{
$image = imagecreatefromjpeg($file->getRealPath());
switch ($orientation) {
case 2:
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3:
$image = imagerotate($image, 180, 0);
break;
case 4:
$image = imagerotate($image, 180, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 5:
$image = imagerotate($image, -90, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 7:
$image = imagerotate($image, 90, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
imagejpeg($image, $file->getRealPath(), 90);
return new UploadedFile($file->getRealPath(), $file->getClientOriginalName(), $file->getMimeType(), $file->getSize());
}
示例2: handleUploadedFile
/**
* Handles an uploaded file by putting it in the $targetDir.
*
* @param UploadedFile $uploadedFile File object that has been uploaded - usually taken from Request object.
* @param string $targetDir [optional] Where to (relatively to the storage root dir) put the file?
* @param array $allowed [optional] What files are allowed? If not matching then will throw exception.
* @param int $maxFileSize [optional] What is the maximum allowed file size for this file?
* @return File
*/
public function handleUploadedFile(UploadedFile $uploadedFile, $targetDir = '/', array $allowed = array(), $maxFileSize = 0)
{
array_walk($allowed, function ($ext) {
return strtolower($ext);
});
$targetDir = trim($targetDir, '/');
$targetDir = $this->path . $targetDir . (empty($targetDir) ? '' : '/');
$filenameElements = explode('.', $uploadedFile->getClientOriginalName());
$extension = array_pop($filenameElements);
$extension = strtolower($extension);
$filename = implode('.', $filenameElements);
$targetName = StringUtils::fileNameFriendly($filename . '-' . StringUtils::random() . '.' . $extension);
$targetPath = $targetDir . $targetName;
// create unique file name
while (file_exists($targetPath)) {
$targetName = StringUtils::fileNameFriendly($filename . '-' . StringUtils::random() . '.' . $extension);
$targetPath = $targetDir . $targetName;
}
// basic check for allowed type
if (!empty($allowed) && !in_array($extension, $allowed)) {
throw new FileException('The uploaded file is not of a valid type (allowed: ' . implode(', ', $allowed) . ').');
}
// basic check for max allowed size
if ($maxFileSize && $uploadedFile->getSize() > $maxFileSize) {
throw new FileException('The uploaded file is too big (max allowed size is ' . StringUtils::bytesToString($maxFileSize) . ').');
}
try {
$movedFile = $uploadedFile->move(rtrim($targetDir, '/'), $targetName);
} catch (SfFileException $e) {
// if exception thrown then convert it to our exception
throw new FileException($e->getMessage(), $e->getCode());
}
$file = $this->convertSfFileToStorageFile($movedFile);
return $file;
}
示例3: convertFieldValueFromForm
/**
* {@inheritdoc}
*
* @param null|\Symfony\Component\HttpFoundation\File\UploadedFile $data
*/
public function convertFieldValueFromForm($data)
{
if ($data === null) {
return null;
}
return array("inputUri" => $data->getRealPath(), "fileName" => $data->getClientOriginalName(), "fileSize" => $data->getSize());
}
示例4: testGetSize
public function testGetSize()
{
$file = new UploadedFile(__DIR__ . '/Fixtures/test.gif', 'original.gif', 'image/gif', filesize(__DIR__ . '/Fixtures/test.gif'), null);
$this->assertEquals(filesize(__DIR__ . '/Fixtures/test.gif'), $file->getSize());
$file = new UploadedFile(__DIR__ . '/Fixtures/test', 'original.gif', 'image/gif');
$this->assertEquals(filesize(__DIR__ . '/Fixtures/test'), $file->getSize());
}
示例5: process
public function process(UploadedFile $file)
{
// File extension
$this->extension = $file->getClientOriginalExtension();
// Mimetype for the file
$this->mimetype = $file->getMimeType();
// Current user or 0
$this->user_id = Auth::user() ? Auth::user()->id : 0;
$this->size = $file->getSize();
list($this->path, $this->filename) = $this->upload($file);
$this->save();
// Check to see if image thumbnail generation is enabled
if (static::$app['config']->get('cabinet::image_manipulation')) {
$thumbnails = $this->generateThumbnails($this->path, $this->filename);
$uploads = array();
foreach ($thumbnails as $thumbnail) {
$upload = new $this();
$upload->filename = $thumbnail->fileSystemName;
$upload->path = static::$app['config']->get('cabinet::upload_folder_public_path') . $this->dateFolderPath . $thumbnail->fileSystemName;
// File extension
$upload->extension = $thumbnail->getClientOriginalExtension();
// Mimetype for the file
$upload->mimetype = $thumbnail->getMimeType();
// Current user or 0
$upload->user_id = $this->user_id;
$upload->size = $thumbnail->getSize();
$upload->parent_id = $this->id;
$upload->save();
$uploads[] = $upload;
}
$this->children = $uploads;
}
}
示例6: validateMaxSize
public function validateMaxSize($attribute, UploadedFile $value, $parameters)
{
$mediaPath = public_path(config('asgard.media.config.files-path'));
$folderSize = $this->getDirSize($mediaPath);
preg_match('/([0-9]+)/', $folderSize, $match);
return $match[0] + $value->getSize() < config('asgard.media.config.max-total-size');
}
示例7: uploadFile
/**
* Upload provided file and create matching FileRecord object
*
* @param UploadedFile $file
* @param $clientIp
* @return FileRecord
*/
public function uploadFile(UploadedFile $file, $clientIp)
{
$extension = Str::lower($file->getClientOriginalExtension());
$generatedName = $this->generateName($extension);
// Create SHA-256 hash of file
$fileHash = hash_file('sha256', $file->getPathname());
// Check if file already exists
$existingFile = FileRecord::where('hash', '=', $fileHash)->first();
if ($existingFile) {
return $existingFile;
}
// Query previous scans in VirusTotal for this file
if (config('virustotal.enabled') === true) {
$this->checkVirusTotalForHash($fileHash);
}
// Get filesize
$filesize = $file->getSize();
// Check max upload size
$maxUploadSize = config('upload.max_size');
if ($filesize > $maxUploadSize) {
throw new MaxUploadSizeException();
}
// Move the file
$uploadDirectory = config('upload.directory');
$file->move($uploadDirectory, $generatedName);
// Create the record
/** @var FileRecord $record */
$record = FileRecord::create(['client_name' => $file->getClientOriginalName(), 'generated_name' => $generatedName, 'filesize' => $filesize, 'hash' => $fileHash, 'uploaded_by_ip' => $clientIp]);
return $record;
}
示例8: getFileArray
/**
* converts UploadedFile to $_FILES array
*
* @return array
*/
public function getFileArray()
{
$array = array('name' => $this->uploaded_file->getClientOriginalName(), 'type' => $this->uploaded_file->getClientMimeType(), 'tmp_name' => $this->uploaded_file->getPath() . $this->getOSDirectorySeparator() . $this->uploaded_file->getFilename(), 'error' => $this->uploaded_file->getError(), 'size' => $this->uploaded_file->getSize(), 'dimension' => array('width' => 0, 'height' => 0));
if (preg_match('/^image/', $array['type'])) {
list($array['dimension']['width'], $array['dimension']['height']) = getimagesize($this->uploaded_file);
}
return $array;
}
示例9: validate
/**
* Checks if the passed value is valid.
*
* @param UploadedFile $file The value that should be validated
* @param Constraint $constraint The constraint for the validation
*/
public function validate($file, Constraint $constraint)
{
if (!in_array($file->getMimeType(), $this->mimeTypes)) {
$this->context->buildViolation($constraint->messageMimeTypes)->atPath('file')->addViolation();
}
if ($file->getSize() > $file->getMaxFilesize()) {
$this->context->buildViolation($constraint->messageMaxSize, array('%max_size%' => $file->getMaxFilesize()))->atPath('file')->addViolation();
}
}
示例10: determineFilesizeLimit
/**
* Determines if the file is too large
*
* @throws Exception
*/
private function determineFilesizeLimit()
{
$php_ini = ini_get('upload_max_filesize');
$mb = str_replace('M', '', $php_ini);
$bytes = $mb * 1048576;
if ($this->file->getSize() > $bytes) {
throw new \Exception('File too large');
}
}
示例11: setupFile
/**
* Setup image file
*
* @param UploadedFile $file
* @param string $style_guide
*/
public function setupFile(UploadedFile $file, $style_guide = null)
{
$this->_source = $file;
$this->setFileExtension($file->getClientOriginalExtension());
$this->setFileNameAttribute($file->getClientOriginalName());
$this->setMimeTypeAttribute($file->getClientMimeType());
$this->setFileSizeAttribute($file->getSize());
if (!empty($style_guide)) {
$this->setStyleGuideName($style_guide);
}
}
示例12: putUploadedFile
/**
* @param UploadedFile $uploadedFile
* @param FileEntity $fileEntity
* @return bool
*/
public function putUploadedFile(UploadedFile $uploadedFile, FileEntity $fileEntity)
{
list($path, $filesystem, $settings) = $this->getConfigValuesForRecordType($fileEntity->getRecordType());
$fullPath = $path . DIRECTORY_SEPARATOR . $fileEntity->getFileName();
$this->getFilesystem($filesystem)->putStream($fullPath, fopen($uploadedFile->getPathname(), 'r'), $settings);
$fileEntity->setContentType($uploadedFile->getMimeType())->setSize($uploadedFile->getSize());
/** @var EntityManager $em */
$em = $this->container['orm.em'];
$em->flush();
return $fileEntity;
}
示例13: add
/**
* @param UploadedFile $file
* @return Document
*/
public function add(UploadedFile $file)
{
$document = new Document();
$document->setExtension($file->guessExtension())->setMime($file->getMimeType())->setName($file->getClientOriginalName())->setSize($file->getSize());
if (is_null($document->getExtension())) {
$document->setExtension($file->getClientOriginalExtension());
}
$this->em->persist($document);
$this->em->flush();
$file->move($this->directory . '/' . substr($document->getId(), 0, 1), $document->getId() . '.' . $document->getExtension());
return $document;
}
示例14: getFile
/**
* @param string $email
* @return UploadedFile
* @throws InvalidArgumentException
*/
public function getFile($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException();
}
$hash = md5(strtolower(trim($email)));
$guzzle = new Client();
$tempDirectory = sys_get_temp_dir();
$guzzle->request("GET", "http://gravatar.com/avatar/" . $hash . "?d=404", [RequestOptions::SINK => $tempDirectory . '/' . $hash]);
$file = new File($tempDirectory . '/' . $hash);
$fileOriginalName = $hash . '.' . $file->guessExtension();
$file = new UploadedFile($tempDirectory . '/' . $hash, $fileOriginalName, $file->getMimeType(), $file->getSize(), null, true);
return $file;
}
示例15: createFromUploadedFile
/**
* @param FileInterface $storageFile
* @param UploadedFile $uploadData
*/
public function createFromUploadedFile(FileInterface $storageFile, UploadedFile $uploadData)
{
$fileSystem = new Filesystem();
$storageFile->setFileName($uploadData->getClientOriginalName());
$storageFile->setMimeType($uploadData->getMimeType());
$storageFile->setSize($uploadData->getSize());
if (!$storageFile->getId()) {
$this->save($storageFile);
} else {
$fileSystem->remove($this->getFilePath($storageFile));
}
$storageFile->setStorageId(uniqid($storageFile->getId() . '_', false));
$uploadData->move($this->getStorageDirectory($storageFile), $storageFile->getStorageId());
$this->save($storageFile);
}