对于一些技术问题的文件类型,我无法从我的输入将文件发送到我的后端的传统方式。我必须从文件读取数据,将其绑定到模型并将该数据(作为一个巨大的字符串)发送到服务器。比方说,我的用户上传JPG格式,我的代码看起来是这样的:有没有办法知道,只有数据
// This return the data in the form of a simple string.
$data = $this->getRequest()->request->get('document[document_file]', null, true);
// Let's assume that I know what type of file my user send me
$data = explode("data:image/jpeg;base64,", $file);
$data = base64_decode($data[1]);
$image = imagecreatefromstring($data);
imagejpeg($image, "test.jpg");
我的问题是,我不知道是什么类型的文件我的用户会上传。有没有办法找出它是什么类型的文件,并以更有效的方式保存它。处理这样一个问题的更有效的方式是什么?
编辑:有一点需要注意的是,它并不总是图像。它可以是.pdf,.doc,.txt!
试着看一下FileInfo的扩展:http://php.net/manual/pl/book.fileinfo.php,或者如果你期望的图像,你可以尝试使用检查文件'imagecreatefromstring ()''''getimagesizefromstring()'等,并检查错误。 – piotrekkr
@piotrekkr我不知道这是否是一个图像,它可能是一个.doc,txt文件,.PDF,我会在FileInfo的 –
我写'如果你希望image',如果你不确信我们来看一看: ) – piotrekkr