Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何从大文件中删除最后N个字节?_Php_Large Files - Fatal编程技术网

PHP:如何从大文件中删除最后N个字节?

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

我有一个大文件,需要删除最后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

$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字节,它不是把整个文件都加载到内存中了吗?