PHP:如何从大文件中删除最后N个字节?
我有一个大文件,需要删除最后512字节。我不想复制文件PHP:如何从大文件中删除最后N个字节?,php,large-files,Php,Large Files,我有一个大文件,需要删除最后512字节。我不想复制文件 谢谢。我还没有用大文件测试过,但你可以试试: 您应该使用ftruncate(句柄,文件大小-512)(使用filesize或fstat函数获取文件大小)使用fstat、ftruncate、fopen和fclose的用法示例: <?php $bytesToTruncate = 5; // how many bytes we are going to delete from the end of the file $ha
谢谢。我还没有用大文件测试过,但你可以试试:
ftruncate(句柄,文件大小-512)
(使用filesize
或fstat
函数获取文件大小)使用fstat
、ftruncate
、fopen
和fclose
的用法示例:
<?php
$bytesToTruncate = 5; // how many bytes we are going to delete from the end of the file
$handle = fopen('/home/francesco/mytest', 'r+'); // Open for reading and writing; place the file pointer at the beginning of the file.
$stat = fstat($handle);
$size = $stat['size'] - $bytesToTruncate;
$size = $size < 0 ? 0 : $size;
ftruncate($handle, $size);
fclose($handle);
你能把文件读入内存吗?@alexn:可能不是个好主意。@BoltClock大到不能复制它,或者@alexn不能把文件读入内存memory@BoltClock可能不会,但是,如果我们不知道文件大小和内存限制,我们怎么能确定呢?@alexn如果我们不知道文件大小和内存限制,这不是一个好主意sure@profitphp我还没有用真正的大文件测试过它,但是用200-300 Mb的文件就可以了。除了最后的512字节,它不是把整个文件都加载到内存中了吗?