Reputation: 1502
I am using UploadedFile
class in symfony2 to upload image files. I have allowed images with mime type image/jpg
and image/png
. When I upload a png file from my system to server its mime type is changed automatically. I know its changed because when I fire this command:
file --mime-type /home/mysuer/img.png
it gives me this:
home/myuser/img.png: image/png
but when I upload file and print UploadedFile
object it gives following output:
Array
(
[media] => Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => img.png
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1246
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => /tmp/phpivgnne
[fileName:SplFileInfo:private] => phpivgnne
)
)
Can any one suggest what's wrong?
Here is my Code:
public function postFileAction(Request $request)
{
print_r($request->files->all());//Prints All files
$image = $request->files->get('media');
echo $image->getClientMimeType();exit; //Mime Type Information
if (empty($image)) {
throw new FileException('Invalid File, Please Select a Valid File.');
}
$uploader = $this->get('application.file_uploader');
if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
//authenticated (NON anonymous users)
throw new InvalidParameterException('Invalid User, Please use valid credentials');
}
return $uploader->upload($image, $this->get('security.context')->getToken()->getUser()->getId());
}
PHPUnit Test Code
public function testPostFile()
{
$file = tempnam(sys_get_temp_dir(), 'upl'); // create file
imagepng(imagecreatetruecolor(10, 10), $file); // create and write image/png to it
$image = new UploadedFile($file, 'new_image.png', 'image/png', 10, UPLOAD_ERR_OK);
$crawler = $this->client->request(
'POST', '/api/v1/files.json', array('submit' => 'Submit Form'), array('media' => $image)
);
$response = $this->client->getResponse();
$this->assertJsonResponse($response);
$this->assertContains('filename', $response->getContent());
print_r($response->getContent());exit;
$fileToken = json_decode($response->getContent());
$this->assertNotEmpty($fileToken->filename);
unlink($file);
unset($file, $image, $crawler, $response);
return $fileToken->filename;
}
I am using curl
from command prompt to test my REST web service file upload like this:
curl -X POST myapi.com/app_dev.php/api/v1/files.json --form "media=@/home/myuser/img.png"
Upvotes: 3
Views: 14504
Reputation: 1502
Finally I got the solutions to my problem will post the answer for problem in both for core-php and for symfony2 also.
Core PHP(found on http://php.net/manual/en/features.file-upload.php):
// DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['upfile']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
So Never trust $_FILES['upfile']['mime']
always use fileinfo
and this have given me the clue.
for Symfony2:
So I started looking into my code and found that I am using $file->getClientMimeType()
, which is not good we have to use $file->getMimeType()
which uses fileinfo
to fetch mime type and will give the correct mime type.
In getMimeType()
The mime type is guessed using a MimeTypeGuesser
instance, which uses finfo()
, mime_content_type()
and the system binary "file" (in this order), depending on which of those are available.
Thanks Everyone for the Help :D
Upvotes: 8