Text file to .Dat file Conversion in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jebaraj
Forum Newbie
Posts: 4
Joined: Mon Nov 23, 2009 7:44 am

Text file to .Dat file Conversion in PHP

Post by jebaraj »

Hi All,
Could anybody provide code for Text file to .Dat file Conversion in PHP. :banghead:

Thanks,
Jeba Raj.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Text file to .Dat file Conversion in PHP

Post by AbraCadaver »

Code: Select all

copy('filename.txt', 'filename.dat');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jebaraj
Forum Newbie
Posts: 4
Joined: Mon Nov 23, 2009 7:44 am

Re: Text file to .Dat file Conversion in PHP

Post by jebaraj »

If we use "copy('filename.txt', 'filename.dat');" both .txt and .dat files have same content. But i need .dat should be in binary coded format.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Text file to .Dat file Conversion in PHP

Post by AbraCadaver »

jebaraj wrote:If we use "copy('filename.txt', 'filename.dat');" both .txt and .dat files have same content. But i need .dat should be in binary coded format.
Yes, that was an important bit of information wasn't it? Try and assume that no one has any idea what your .dat file is or what the format is. :idea:

Code: Select all

$txt = file_get_contents('filename.txt');
 
$bin = "";
for($i=0; $i < strlen($txt); $i++ ){
    $bin .= sprintf("%08s ", decbin(ord($txt[$i])));
}
 
file_put_contents('filename.dat', $bin);
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jebaraj
Forum Newbie
Posts: 4
Joined: Mon Nov 23, 2009 7:44 am

Re: Text file to .Dat file Conversion in PHP

Post by jebaraj »

Hi,
Thanks for quick reply. if i use decbin() it will convert to 0's and 1's. But i need .dat file format as "http://www.cooketech.co.uk/convertor/Te ... 20FORM.dat".

Can any one help me, what format of file is this .dat file(Binary or ASCII or...)?

I need text files to be converted like above .dat format!

Thanks,
Jeba Raj
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Text file to .Dat file Conversion in PHP

Post by AbraCadaver »

jebaraj wrote:Hi,
Thanks for quick reply. if i use decbin() it will convert to 0's and 1's. But i need .dat file format as "http://www.cooketech.co.uk/convertor/Te ... 20FORM.dat".

Can any one help me, what format of file is this .dat file(Binary or ASCII or...)?

I need text files to be converted like above .dat format!

Thanks,
Jeba Raj

Ah ha! Now we are getting some details. One at a time, but we have some. Do you happen to know what application creates the .dat file or if there is a specification for the .dat file somewhere? If I were to ask you how to convert a .txt file to a .bob file, what would you say?

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jebaraj
Forum Newbie
Posts: 4
Joined: Mon Nov 23, 2009 7:44 am

Re: Text file to .Dat file Conversion in PHP

Post by jebaraj »

Hi,

Thank for u r reply.Sony camera creates this .dat file!

Thanks,
Jeba Raj
Post Reply