|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-04-29 11:36 UTC] techtonik@php.net
Description: ------------ Note or warning here? http://www.php.net/manual/en/function.ftruncate.php Reproduce code: --------------- <?php // truncate doesn't move the file pointer to the end of the file $flog = fopen("text.txt", "w+"); fwrite($flog, "veeery veeery long string"); fseek($flog, -2, SEEK_END); ftruncate($flog, 6); fwrite($flog, "shortstr"); fclose($flog); ?> Expected result: ---------------- 0000000000: 76 65 65 65 72 79 20 73 │ 68 6F 72 74 73 74 72 veeery shortstr Actual result: -------------- 0000000000: 76 65 65 65 72 79 20 00 │ 00 00 00 00 00 00 00 00 veeery 0000000010: 00 00 00 00 00 00 00 73 │ 68 6F 72 74 73 74 72 shortstr PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 23:00:01 2025 UTC |
Something like "Warning: Truncate doesn't change position of the file pointer. Adjust the pointer with fseek to write to the end of the file after truncate operation in modes other than "a" and "a+"." ? Examples: <?php $fh = fopen("file.txt","w+"); fwrite($fh, "newfile"); // file contains string 'newfile' ftruncate($fh, 3); fseek($fh, 0, SEEK_END); fwrite($fh, "contents"); // file contains 'newcontents' fclose($fh); ?> <?php $fh = fopen("file.txt","w+"); fwrite($fh, "newfile"); // file contains string 'newfile' ftruncate($fh, 3); fwrite($fh, "contents"); // file contains 'new contents' fclose($fh); ?>