How to edit large files in Binary using php

Post Reply
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

How to edit large files in Binary using php

Post by Rksk » Sun Dec 05, 2010 5:58 pm

Experts,

I want to replace a set of binary charators in some files (1-2MB sized) using php.

I tried below functions. but they got overflow when loading file to variable.

Code: Select all

    function gethex()
    {
    $file = 'source.mp3';
        $handle = fopen($file, 'r') or die('permission?');
        
            while(!feof($handle))
            {
                foreach(unpack('C*',fgets($handle)) as $dec)
                {
                    $tmp = dechex($dec);
                    $this->hex[] .= strtoupper(str_repeat('0',2-strlen($tmp)).$tmp);    
                }
            }
        
        return join($this->hex);
    }
    
//****************************************

    function writehex($hexcode)
    {
        $file = 'test.mp3';
        foreach(str_split($hexcode,2) as $hex)
        {
            $tmp .= pack('C*', hexdec($hex));
        }
        
            $handle = fopen($file, 'w+') or die('permission?');
            fwrite($handle, $tmp);
        
    } 


Please tell me another way to do it.

Thankz.

[ Post made via Mobile Device ] Image
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to edit large files in Binary using php

Post by Saman » Mon Dec 06, 2010 12:24 am

User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to edit large files in Binary using php

Post by Rksk » Mon Dec 06, 2010 9:59 pm

Thankz bro.
it worked for me.

[ Post made via Mobile Device ] Image
Post Reply

Return to “PHP & MySQL”