mowwwalker
mowwwalker

Reputation: 17334

Handling Reading/Writing Files in PHP?

It's been a while since I've touched PHP, and I've been working in C# for a while. I need to do some file reading/writing, but I'm not sure where to start. I've been spoiled by Visual Studio's code-completion and real-time error checking, and it's a bit difficult going over to such a weakly-typed language.

In PHP, what's returned when reading a file, and what needs to be written when writing?

I need to work with the file in hex, but decimal would be fine too. Is there any way to read it in any way but a string?

Upvotes: 0

Views: 90

Answers (1)

devdRew
devdRew

Reputation: 4561

There is a several ways to read and write files:

You can create a handler by fopen() function.

The other way is just file_get_contents(), this function just returns content. And file_put_contents() just put any data to file.

As example of the handler, here is a logging stuff:

    if (!is_writable($this->file) && $name !== self::CORE_LOG)
    {
        self::getInstance(self::CORE_LOG)->log(sprintf('Couldn\'t write to file %s. Please, check file credentials.', $name));
    }
    else
    {
        $this->handler = fopen($this->file, 'a+');
        self::$instances[$name] = &$this;
    }
...

    if ($this->handler)
        fwrite($this->handler, '[' . date('r') . '] : ' . $l . "\n");
...

    if ($this->handler)
        fclose($this->handler);

Here you can read about and Filesystem managment functions

Upvotes: 1

Related Questions