/    Sign up×
Community /Pin to ProfileBookmark

Reading file in as binary data

I’m trying to use the [url=http://www.zend.com/codex.php?id=696&single=1]Zend ZIP class[/url] and at some point I have to read my file into a binary data string and then store it into the Zip file. This may be a very simple process, but I don’t know how to do it. The documentation at Zend just says

[code]
//add the binary data stored in the string ‘filedata’
$filedata = “(read your file into $filedata)”;
$zipfile -> add_file($filedata, “dir/file.txt”);
[/code]

The files are of varying types : text, jpg, tif, xls, etc…

I’ve looked around various sites including this one, but I may be typing in the wrong search words. In anycase, can anyone give me a clue?

Thanks a mill,
– sharyn

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@yunaNov 12.2004 — If you have a recent version of PHP (4.3.0 or later), use file_get_contents() as documented here:

http://us2.php.net/manual/en/function.file-get-contents.php

In earlier versions, you need to open the file with fopen(), then use fread() like this example from the manual page for fread():

[code=php]
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
[/code]


Both these functions are binary-safe.
Copy linkTweet thisAlerts:
@sharynauthorNov 15.2004 — Yuna,

Thanks for your help. It turns out file_get_contents() worked exactly the way I needed ?

  • - sharyn
  • ×

    Success!

    Help @sharyn spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...