【问题标题】:PHP need to set size of FilePHP需要设置文件的大小
【发布时间】:2021-06-22 13:45:50
【问题描述】:

如何设置文件的当前大小?

在我的代码中,我首先执行fopen( $inPath, "r+" );。 其次做fwrite($mFile, $mPage, $kLicenceSize);

$mFile -> 我打开的文件,

$mPage -> 我应该写入文件的数据,

$kLicenceSize -> 4096 (4 KB)。

因此,我应该得到一个 4KB 的文件,其中包含前面提到的数据。顺便说一句,数据可以小于 4KB。

【问题讨论】:

  • 真的不知道你到底想问我们什么。请尝试使问题易于理解。如果英语是您的问题,请使用 Google 翻译,用您自己的语言提出问题,然后向我们展示英语转换
  • @RiggsFolly 已编辑
  • 你还没有告诉我们问题是什么
  • 请给我们看真实的代码,我们是开发者,我们懂代码
  • 来自fwrite()手册If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, **whichever comes first**.所以它将在mPage字节数之后停止,它不会发明字节来将文件打包到4k

标签: php file size


【解决方案1】:

fwrite() 不是这样工作的。来自文档:https://www.php.net/manual/en/function.fwrite.php

长度

If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

通过在您的 fwrite() 调用中包含 length 参数,您可以确保写入的文件永远不会超过您的 length (4kb),您不会将文件的大小设置为 length

基于 cmets 编辑 你可以使用str_pad():

$paddedFile = str_pad( $mFile, $kLicenceSize );
fwrite($paddedFile, $mPage, $kLicenceSize);

【讨论】:

  • 是的,我知道。我的问题是“即使数据较小,如何创建 4K 文件?”
  • 您必须确定数据的长度,然后填充数据以满足所需的长度。用例是什么?
  • @Rostislav How can I create a 4K file even if the data is smaller?...除了浪费磁盘空间还有什么意义?
  • @ADyson 我只是创建了一个项目,它首先生成一个带有串行密钥数据的 4Kb 文件。然后这个文件被加密并发送给买家。这就是这个程序的目的
  • @Rostislav 当然,但是为什么它必须是 4KB 呢?为什么不更小?
猜你喜欢
  • 2021-07-16
  • 2019-08-04
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2021-07-20
相关资源
最近更新 更多