Reputation: 2535
I have a photo upload HTML form where its values are captured using getRequest(). How can I grab the file extension of this when I am not using Symfony forms? Here's the line I catch the form element value.
$image = $this->getRequest()->files->get("image");
Upvotes: 1
Views: 9598
Reputation: 166
function importFile(Request $request){
$files = $request->files->get('image'); // Get file object
echo $files->getClientOriginalName(); // Get File Name
echo $files->getClientOriginalExtension(); // Get Extension
dd($files);
}
Upvotes: 0
Reputation: 176
If you need to know the file extention you can use getClientOriginalExtension() to get the file type extention. Like this way:
$image = $request->files->get( 'image' );
print_r($image->getClientOriginalExtension());
There are other function you can you to get other detail like file size, file type,file path and other they are
getATime()
getBasename()
getCTime()
getClientMimeType()
getClientOriginalExtension()
getClientOriginalName()
getClientSize()
getError()
getErrorMessage()
getExtension()
getFileInfo()
getFilename()
getGroup()
getInode()
getLinkTarget()
getMTime()
getMaxFilesize()
getMimeType()
getOwner()
getPath()
getPathInfo()
getPathname()
getPerms()
getRealPath()
getSize()
getType()
and so on.
Upvotes: 5
Reputation: 2535
getClientOriginalName()
worked for me to get the original file name. guessEstension()
did not work.
Upvotes: 0
Reputation: 31
You can use symfony's guessExtension() function
$image = $this->getRequest()->files->get("image");
// Symfony is guessing extension of file
$extension = $image->guessExtension();
Upvotes: 3
Reputation: 3483
if you need get extenstion of file before upload file
This is your Entity file
class Excel_file_details
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="filename", type="string", length=255)
*/
private $filename;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setFilename($filename)
{
$this->filename = $filename;
return $this;
}
public function getFilename()
{
return $this->filename;
}
}
In your controller
$excelobj = new Excel_file_details();
$uploaddir = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/excelfiles/' . $folderName;
$file = $excelobj->getFilename();
$fileName = $excelobj->getFilename()->getClientOriginalName();
$ext = pathinfo($uploaddir . $fileName, PATHINFO_EXTENSION);
$name = substr($fileName, 0, -(strlen($ext) + 1));
$fileName = $name . '@' . date('Y-m-d H-i-s') . '.' . $ext;
Upvotes: 0
Reputation: 3483
$uploadPath = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/';
try {
$uploadfile = $request->files->get('filename');
/* @var $uploadfile \Symfony\Component\HttpFoundation\File\File */
$uploadedFile = $uploadfile->move($uploadPath, $uploadfile->getClientOriginalName());
unset($uploadfile);
} catch (\Exception $e) {
/* if you don't set $avatarFile to a default file here
* you cannot execute the next instruction.
*/
}
$filename = $uploadedFile->getBasename();
$extentison = $uploadedFile->getExtension();
Upvotes: 0