.txt PHP 文件上传 txt 扩展名?
我正在用 php 进行文件上传。 .txt 是上传过滤器的有效扩展名吗?如果可以的话我可以看一个例子吗?
编辑:抱歉我不太清楚。我的意思是我只希望用户能够上传 .txt 文件,仅此而已。
I am doing a file upload in php. Is .txt a valid extension to upload filters? If so may I see an example?
EDIT: I'm sorry I'm not too clear. What I mean is I only want the user to be able to upload .txt files and that is all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
任何东西都是有效的,直到您在上传文件时使其无效为止。使用此代码,您将看到哪些属性使 PHP 的文件处理生效:
upload.html:
upload_file.php:
我不太确定您在问什么,所以稍微澄清一下就好了。
我不使用 PHP (Python FTW),但如果您想确保只有文本文件通过,请过滤它们:
我不确定
text/plain
是否会导致漏报,所以如果有,只需将其删除即可。Anything's valid until you make it invalid when it comes to uploading files. Play with this code and you'll see what properties make PHP's file processing tick:
upload.html:
upload_file.php:
I'm not really sure what you're asking, so a bit clarification would be nice.
I don't work with PHP (Python FTW), but if you want to make sure only text files get through, filter them:
I'm not sure if the
text/plain
will cause false-negatives, so if it does, just remove it.默认情况下,除非您使用某种第三方库,否则任何文件都可以上传。你用它做什么,解析它,是另一个故事了:)
By default, unless you're using a 3rd-party library of some sorts, any file can be uploaded. What you do with it, parsing it, is another story :)
使用表单中提供的文件名,您必须提取文件扩展名。然后,您将文件扩展名与扩展名列表(您允许的)进行比较,如果与其中任何扩展名都不匹配,则不处理该文件。
首先提取扩展名并
使用 case 语句比较结果,或者(因为您有 1 个允许的扩展名)如果
Using the supplied filename from your form, you must extract the file extension. You then compare the file extension to a list of extensions (that you allow) and if it does not match any of them, do not process the file.
First extract the extension by
and compare the result using a case statement or (since you have 1 allowed extension) just if