php读取文件夹返回为空,为什么php file_info为空文本文件返回inode / x-empty?(Why does php file_info return inode/x-empty fo...

为什么php file_info为空文本文件返回inode / x-empty?(Why does php file_info return inode/x-empty for empty text files?)

我目前正在开发一个包装finfo_file函数的类。

以下脚本为所有空文本文件返回inode/x-empty :

$finfo = finfo_open(FILEINFO_MIME_TYPE);

echo finfo_file($finfo,'/path/to/text_file.txt');

在以下环境中测试并收到相同的结果。

WAMP

Windows 7的

PHP 5.4.27

APACHE 2.2.22

Linux的

Ubuntu的

PHP 5.5.10

APACHE 2.4.9

我的目标是设置类,以便我可以列出文件类型。 如果text/plain的MIME类型是白名单,则空文本文件将失败,因为它返回MIME类型inode/x-empty 。

这是finfo_file函数的默认行为吗?

I am currently working on a class that wraps the finfo_file function.

The following script returns inode/x-empty for all empty text files:

$finfo = finfo_open(FILEINFO_MIME_TYPE);

echo finfo_file($finfo,'/path/to/text_file.txt');

Tested in the following environments and received the same results.

WAMP

Windows 7

PHP 5.4.27

APACHE 2.2.22

Linux

Ubuntu

PHP 5.5.10

APACHE 2.4.9

My goal is to setup the class so that I can white list file types. If a MIME type of text/plain is white listed, the empty text file would fail since it returns a MIME type of inode/x-empty.

Is this default behavior for the finfo_file function?

原文:https://stackoverflow.com/questions/24015038

更新时间:2020-01-21 11:00

最满意答案

是的,因为文件扩展名没有定义文件类型。 例如,您可以将视频文件的扩展名从.mp4更改为.txt,并仍在播放器中播放视频。 Windows确实更严格地处理文件扩展,但在unix系统中,扩展更像是用户的类型提示,然后它实际上意味着系统本身的某些东西(尽管有例外)。

文件信息函数查看文件的内容,并尝试根据它在那里找到的mime类型来确定。

如果你想白名单text / plain,还有空文本文件,你可以使用pathinfo()做这样的事情:

if($mime_type == 'inode/x-empty' && pathinfo($file_name, PATHINFO_EXTENSION) == 'txt') {

// whitelist

}

Yes, because the file extension doesn't define the file type. You can, in example, change the extension of a video file from .mp4 to .txt, and still play the video in a player. Windows does handle file extensions a bit more strictly, but in unix systems the extension is more like a type-hint for users then that it really means something for the system itself (exceptions are there though).

The file info functions look to the contents of a file and try to determine the mime-type from what it finds there.

If you want to white-list text/plain, but also empty text files, you could do something like this, using pathinfo():

if($mime_type == 'inode/x-empty' && pathinfo($file_name, PATHINFO_EXTENSION) == 'txt') {

// whitelist

}

2014-06-03

相关问答

正如编辑的问题中所提到的,当PHP默默丢弃数据时(发生在实际数据大于post_max_size时),$ _POST和$ _FILES是空的。 由于HTTP头和$ _GET保持不变,可用于检测丢弃。 选项a) if(intval($_SERVER['CONTENT_LENGTH'])>0 && count($_POST)===0){

throw new Exception('PHP discarded POST data because of request exceeding post_m

...

以下是PHP文件上传的检查列表: 检查php.ini: file_uploads = On post_max_size = 100M upload_max_filesize = 100M 您可能需要使用.htaccess或.user.ini如果您处于共享主机,并且无法访问php.ini 。 确保您正在编辑正确的ini文件 - 使用phpinfo()函数来验证您的设置是否实际应用。 还要确保你不要拼写大小 - 它应该是100M 不是 100MB 。 确保您的

标签具有enctype="mu

...

我可能只是使用一行Script task. System.IO.File.Create(@"C:\OneEmptyTextFile.txt");

在您的控制流中,从SSIS工具箱中拖动一个类型为Script Task 双击新的Script Task 在脚本任务编辑器中,单击Edit Script... 在public void Main()部分中,它声明// TODO: Add your code here ,用上面的代码替换该行。 如果您使用的是SSIS 2005或者如果您在2008/2012

...

你的脚本是这样的: foreach(glob('/path/to/dir/*.txt') as $file)

{

readfile($file);

unlink($file);

}

请参见readfile()和glob()手册页。 Your script is just this : foreach(glob('/path/to/dir/*.txt') as $file)

{

readfile($file);

unlink($file);

}

See readf

...

尝试改变 $fclose($file);

对此 fclose($file);

我能够让代码在我的服务器上运行。 我在这里做的唯一更改是文件的路径,该文件与PHP脚本位于同一目录中。 <?php

function GetNoticeLog($strDate){

$logdate = preg_replace("/[^0-9]/", "_", $strDate );

//$strFileName = realpath('debuglogs/enotice_logs').'/ENO

...

是的,因为文件扩展名没有定义文件类型。 例如,您可以将视频文件的扩展名从.mp4更改为.txt,并仍在播放器中播放视频。 Windows确实更严格地处理文件扩展,但在unix系统中,扩展更像是用户的类型提示,然后它实际上意味着系统本身的某些东西(尽管有例外)。 文件信息函数查看文件的内容,并尝试根据它在那里找到的mime类型来确定。 如果你想白名单text / plain,还有空文本文件,你可以使用pathinfo()做这样的事情: if($mime_type == 'inode/x-empty'

...

它可能是文件大小,而不是扩展名。 查看php.ini文件,查看这些值的设置。 如果您的文件大于这些,这可以解释它。 post_max_size=5M

upload_max_filesize=5M

The problem is that PHP is not reading the ZIP files generated by the Java App that produces them. But, any other ZIP file is being read. As I am using

...

在为空白编写链接检查文件之前,例如: $file = "Snip_TrackId.txt";

if (0 < filesize($file)) {

$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!");

echo "

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值