?
Solved

reading and writing binary files

Posted on 2005-03-15
1
Medium Priority
?
194 Views
Last Modified: 2008-03-06
How can I create a binary file using PHP

ie I want to store say these information

1 bytes (record number)
10 bytes (name)


so say i have a record number that is represented by a byte followed by a name that is made up of 10bytes.

How would I create a binary file with this information? Seem like even though I use the"b" mode when openning files the record number gets stored as a string instead of a byte.

I can't use a text file for what I am trying to do.


0
Comment
Question by:rukiman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
davebytes earned 375 total points
ID: 13552654
first, you need to convert data to binary format.  if the record number is supposed to be a number, you need to write out the byte format of the integer (i.e., intval($recordnum)) rather than writing just a string.  Read the following page:
http://www.felixgers.de/teaching/php/fileIO.html

Has a LOT on various file i/o stuff.  Eventually you'll reach the pack(), unpack() functions, helpful if you are writing complex data structures in binary.

second, use fwrite($file, $data, $datalength) variation -- ALWAYS specify the data length writing binary data, otherwise PHP may 'transform' things under the hood.  don't use the string file functions, fgets, fputs, even though they may be aliased.  Stick with fread/fwrite.

That make more sense?

Also, you can use serialize($datastructure) to convert complex arrays and variables into a format suitable for writing into an ascii text file, and read it back with unserialize($data).  Just fyi.

-d
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question