note 128111 added to function.fopen

From: Date: Fri, 13 Jan 2023 18:32:19 +0000
Subject: note 128111 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-220065@lists.php.net to get a copy of this message
Opening a file in "r+" mode, and then trying to set the file pointer position with ftruncate before reading the file will result in file data loss, as though you opened the file in "w" mode. EX: $File = fopen($FilePath,"r+"); // OPEN FILE IN READ-WRITE ftruncate($File, 0); // SET POINTER POSITION (Will Erase Data) while(! feof($File)) { // CONTINUE UNTIL END OF FILE IS REACHED $Line = fgets($File); // GET A LINE FROM THE FILE INTO STRING $Line = trim($Line); // TRIM STRING OF NEW LINE } ftruncate($File,0); // (Will Not Erase Data) fclose($File); ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.158.213 (proxied: 2604:3d09:6e7f:bc90:c81a:8b5c:ee4c:1b2b) ---- Manual Page -- https://php.net/manual/en/function.fopen.php Edit -- https://main.php.net/note/edit/128111 Del: integrated -- https://main.php.net/note/delete/128111/integrated Del: useless -- https://main.php.net/note/delete/128111/useless Del: bad code -- https://main.php.net/note/delete/128111/bad+code Del: spam -- https://main.php.net/note/delete/128111/spam Del: non-english -- https://main.php.net/note/delete/128111/non-english Del: in docs -- https://main.php.net/note/delete/128111/in+docs Del: other reasons-- https://main.php.net/note/delete/128111 Reject -- https://main.php.net/note/reject/128111 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220065) next »