Re: ftell ? fseek ? help, please

From: Date: Mon, 09 Feb 2004 15:36:09 +0000
Subject: Re: ftell ? fseek ? help, please
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-177206@lists.php.net to get a copy of this message
On Mon, 2004-02-09 at 07:21, Anna Yamaguchi wrote: > I would like to read first 224 bytes from a file A1, write them to another > file, and then coming back to file A1 read bytes from 225 to the end of > file. > Could somebody help me with this? Please. This is the traditional method: // Open input file $fh = fopen('some_file', 'rb'); // Read header $header_data = fread($fh, 224); // Open header output file $ofh = fopen('some_other_file', 'wb'); // Write header fwrite($ofh, $header_data); // Get rest of file do { $data = fread($fh, 8192); if (strlen($data) == 0) { break; } $contents .= $data; } while (true); fclose($fh); If you know you want the entire file, you can always use file_get_contents. It may behoove you to check the file size first before reading it entirely into memory: if (filesize('some_file') <= 1048576) { $data = file_get_contents('some_file'); $header_data = substr($data, 0, 224); $data = substr($data, 223); } -- Adam Bregenzer adam@bregenzer.net http://adam.bregenzer.net/

« previous php.general (#177206) next »