简体   繁体   English

通过只读取文件的3个字节来检查PHP上传文件在临时文件夹之前的文件类型(FLV)

[英]Check file type (FLV) before PHP upload file in temporary folder by reading only starting 3 bytes of file

First 3 bytes of FLV file are signature "FLV". FLV文件的前3个字节是签名“FLV”。 Now my question: 现在我的问题:

Is there any possibility in PHP to handle file uploads so that we can hook into the input stream of uploading file and check the first 3 bytes? PHP中是否有可能处理文件上传,以便我们可以挂钩上传文件的输入流并检查前3个字节?

The scenario is i don't want the complete file to be uploaded (in temporary folder) and then check if the file is FLV or not, i just want to read first few bytes of uploading stream and if it is not "FLV" then return/exit. 方案是我不希望上传完整的文件(在临时文件夹中),然后检查文件是否是FLV,我只想读取上传流的前几个字节,如果它不是“FLV”那么返回/退出。

The file needs to be uploaded by HTML based form. 该文件需要通过基于HTML的表单上传。 Can't rely on javascript,flash uploader or any client side validation workarounds ie need a pure PHP based solution. 不能依赖javascript,flash上​​传器或任何客户端验证解决方法,即需要一个纯PHP的解决方案。

I already know that PHP does not hand over control to our script until it finishes uploading file in temporary folder and populating global variables ie $_POST $_GET $_FILES etc. 我已经知道PHP不会将控制交给我们的脚本,直到它完成在临时文件夹中上传文件并填充全局变量,即$_POST $_GET $_FILES等。

Also there is a somewhat similar question here: How to upload a file byte by byte in php 此处还有一个类似的问题: 如何在php中逐字节上传文件

but answers does not satisfy my requirement/question. 但答案不符合我的要求/问题。

Any ideas are really appreciated! 任何想法都非常感谢!

Thanks 谢谢

First, set session.upload_progress.enabled in your php.ini . 首先,在php.ini设置session.upload_progress.enabled

Then, use session.upload_progress to track how many bytes have uploaded. 然后,使用session.upload_progress跟踪已上载的字节数。 Once you have reached the minimum threshold, check the temporary file being uploaded, it will be in $_SESSION[unique_key]['files'][0]['tmp_name'] . 达到最低阈值后,检查正在上传的临时文件,它将位于$_SESSION[unique_key]['files'][0]['tmp_name'] If the file doesn't match, set $_SESSION[unique_key]["cancel_upload"] to TRUE , and the file will be rejected. 如果文件不匹配,请将$_SESSION[unique_key]["cancel_upload"]TRUE ,该文件将被拒绝。

To get unique_key : 要获得unique_key

ini_get("session.upload_progress.prefix") . $_POST[ini_get("session.upload_progress.name")];

If the above does not work (I haven't tested it), then your only recourse would be to create your own custom handler for PHP either as an Apache module (or better, as a custom CGI application). 如果上面的方法不起作用(我还没有测试过),那么你唯一的办法就是为Apache创建自己的自定义处理程序(或者更好,作为自定义CGI应用程序)。 There you could do your filtering. 你可以在那里进行过滤。

@burhan-khalid provided a more up to date and correct answer above. @ burhan-khalid提供了一个更新,更正确的答案。

Short Answer is no with your constraints. 简短的答案与你的约束 无关

\n

You can not access that file with PHP until it has been uploaded to the server. 在将文件上载到服务器之前,您无法使用PHP访问该文件。 After it is uploaded you can read it but not before, at least not without some type of client side software that would allow you to stream it to PHP instead of the normal form submission. 上传之后你可以阅读但不是之前,至少不是没有某种类型的客户端软件允许你将它流式传输到PHP而不是普通的表单提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM