本文整理汇总了PHP中Symfony\Component\HttpFoundation\File\UploadedFile类的典型用法代码示例。如果您正苦于以下问题:PHP UploadedFile类的具体用法?PHP UploadedFile怎么用?PHP UploadedFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UploadedFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: 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());
}
示例3: saveFile
/**
* {@inheritdoc}
*/
public function saveFile(UploadedFile $uploadedFile, $fileName)
{
$stream = fopen($uploadedFile->getRealPath(), 'r+');
$result = $this->filesystem->writeStream($this->getMediaBasePath() . '/' . $fileName . '.' . $uploadedFile->guessClientExtension(), $stream);
fclose($stream);
return $result;
}
示例4: putUploadedFile
/**
* Put and save a file in the public directory
*
* @param string path of the file
* @return mixed keypath of file or false if error occurred during uploading
*/
public static function putUploadedFile(UploadedFile $file)
{
if ($file->isValid()) {
//Remove all the slashes that doesn't serve
FileStorage::clearPublicStartPath();
//Retrive and save the file extension of the file uploaded
$fileExtension = $file->getClientOriginalExtension();
//Save the public path with the start path
$absolutePath = public_path() . '/' . FileStorage::$publicStartPath;
//Generate a random name to use for the file uploaded
$keyFile = FileStorage::generateKey(FileStorage::$keyLength) . '.' . $fileExtension;
//Check if the file with the $keyFile name doesn't exist, else, regenerate it
while (file_exists($absolutePath . '/' . ord($keyFile[0]) . '/' . $keyFile)) {
$keyFile = FileStorage::generateKey(FileStorage::$keyLength) . '.' . $fileExtension;
}
//Move the uploaded file and save
$file->move($absolutePath . '/' . ord($keyFile[0]), $keyFile);
//Save the keypath (start path, sub path, file name)
$keyPath = FileStorage::$publicStartPath . '/' . ord($keyFile[0]) . '/' . $keyFile;
//Return public path of the file
return $keyPath;
} else {
return false;
}
}
示例5: saveUserUploadedPhoto
public function saveUserUploadedPhoto(UploadedFile $file)
{
$extension = CommonFunction::getFileExtension($file->getClientOriginalName());
$fileName = md5(microtime(true)) . '.' . $extension;
$file->move($this->getUserPhotoDir(), $fileName);
return $fileName;
}
示例6: updateUserProfileImage
/**
* 회원의 프로필 이미지를 등록한다.
*
* @param UserInterface $user 프로필 이미지를 등록할 회원
* @param UploadedFile $profileFile 프로필 이미지 파일
*
* @return string 등록한 프로필이미지 ID
*/
public function updateUserProfileImage(UserInterface $user, UploadedFile $profileFile)
{
$disk = array_get($this->profileImgConfig, 'storage.disk');
$path = array_get($this->profileImgConfig, 'storage.path');
$size = array_get($this->profileImgConfig, 'size');
// make fitted image
/** @var ImageManager $imageManager */
$imageManager = call_user_func($this->imageManagerResolver);
$image = $imageManager->make($profileFile->getRealPath());
$image = $image->fit($size['width'], $size['height']);
// remove old profile image
if (!empty($user->profileImageId)) {
$file = File::find($user->profileImageId);
if ($file !== null) {
try {
$this->storage->remove($file);
} catch (\Exception $e) {
}
}
}
// save image to storage
$id = $user->getId();
$file = $this->storage->create($image->encode()->getEncoded(), $path . "/{$id}", $id, $disk);
return $file->id;
}
示例7: moveUploadedFile
public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath, $fileName)
{
$originalName = $file->getFilename();
// use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test.
// $relativePath = date('Y-m', filemtime($file->getPath()));
$targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName;
$targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName;
$ext = $file->getExtension();
$i = 1;
while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) {
if ($ext) {
$prev = $i == 1 ? "" : $i;
$targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath);
} else {
$targetFilePath = $targetFilePath . $i++;
}
}
$targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath;
if (!is_dir($targetDir)) {
$ret = mkdir($targetDir, umask(), true);
if (!$ret) {
throw new \RuntimeException("Could not create target directory to move temporary file into.");
}
}
//$file->move($targetDir, basename($targetFilePath));
//$file->move($targetDir, basename($fileName.'.'.$ext));
$file->move($targetDir, basename($fileName));
return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath);
}
示例8: getFileMd5Name
public function getFileMd5Name(UploadedFile $file)
{
$ext = $file->guessExtension();
$md5 = md5_file($file->getRealPath());
$name = $md5 . '.' . $ext;
return $name;
}
示例9: upload
/**
* @param UploadedFile $uploadedFile
*/
function upload(UploadedFile $uploadedFile)
{
$path = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->guessExtension();
$this->setPath($path);
$this->setName($uploadedFile->getClientOriginalName());
$uploadedFile->move($this->getUploadRootDir(), $path);
}
示例10: isFileUpload
/**
* @param string|array|UploadedFile $data
*
* @return bool
*/
protected function isFileUpload($data)
{
if ($data instanceof UploadedFile) {
return $data->isValid() && $data->getClientSize() > 0;
}
return is_array($data) && !empty($data['tmp_name']) && !empty($data['size']) && $data['error'] === UPLOAD_ERR_OK;
}
示例11: saveDisc
private function saveDisc(UploadedFile $file)
{
$fileName = time() . $file->getClientOriginalName();
$path = 'foto/';
$file->move($path, $fileName);
return $fileName;
}
示例12: upload
/**
* The main upload method.
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file Uploaded file instance.
* @return string
*/
public function upload(\Symfony\Component\HttpFoundation\File\UploadedFile $file)
{
// We simply move the uploaded file to the target directory
$result = $file->move(\Config::get('plupload::plupload.upload_dir'), $file->getClientOriginalName());
// Return the result of the upload
return $this->respond(array('OK' => $result ? 1 : 0));
}
示例13: parseUserFile
/**
* Converts UploadedFile of users into array of User objects
*
* @param UploadedFile $file
* @return array
*/
public function parseUserFile(UploadedFile $file)
{
$data = array('users' => array(), 'columns' => array());
switch ($file->getClientOriginalExtension()) {
case 'csv':
$file = $file->openFile();
$data['columns'] = $file->fgetcsv();
while (!$file->eof()) {
$user = new User();
$row = $file->fgetcsv();
if (array(null) == $row) {
continue;
}
foreach ($row as $key => $userProperty) {
$functionName = 'set' . ucfirst($data['columns'][$key]);
if (!method_exists($user, $functionName)) {
throw new MappingException('User has no property ' . $data['columns'][$key]);
}
$user->{$functionName}($userProperty);
}
$this->isUserValid($user);
$data['users'][] = $user;
}
break;
}
return $data;
}
示例14: move
/**
* Move uploaded images to its destination path
* @param UploadedFile $file
* @return $this
*/
public function move(UploadedFile $file)
{
$file->move($this->baseDir, $this->name);
$this->makeThumbnail();
$this->makeIcon();
return $this;
}
示例15: savePhoto
protected function savePhoto(UploadedFile $photo)
{
$fileName = str_random(40) . '.' . $photo->guessClientExtension();
$destinationPath = public_path() . '/upload/gambar/';
$photo->move($destinationPath, $fileName);
return $fileName;
}