Howto open, read and write a file in PHP

Hello world,

this is a very basic operation which i do almost every day and of course you can do it in different ways like always in programming. It's however not for absolute beginners. You need to have some basic knowledge about variables, functions etc. Please visit php.net for these basics, their online documenttation is one of the best of all languages i've worked with.

here is an example how you could do it:

<?php

# OPEN A FILE IN READ ONLY AND PUT RESULT INTO A FILE HANDLER ($FH)
$fh = fopen("/tmp/openme.txt", "r");

Possibiliy 1:

# READ CONTENT AS ONE BIG TEXT STRING FROM FILE HANDLER
$content = file_get_contents($fh);

Possibility 2:

# READ FILE AS AN ARRAY (EVERY LINE GOES TO ONE ARRAY ENTRY)
$content = file($fh);

# IF READ AS AN ARRAY YOU NEED FIRST TO CONVERT THE
# ARRAY INTO A STRING BEFORE SAVING
$content = implode("", $content);

# CREATE A NEW FILE OR OPEN FILE IN WRITE MODE AND GIVE IT
# A NEW FILE HANDLER ($FH_NEW)
$fh_new = fopen("/tmp/saveme.txt", "w");

# NOW STORE THE CONTENT IN THERE
fwrite($content, $fh_new);

# AND DO NOT FORGET TO CLOSE THE FILE AFTERWARDS TO MAKE YOUR FILE PERMANENT
fclose($fh_new);

?>

So, that's it. Hope this helped some of you people out there.

Spindown/Standby HDD (Linux)

Hi, this is for everyone who already wanted to know how to spin down the disks on linux and save some energy. It's quite easy, just enter following command in your terminal:

# hdparm -S 241 /dev/sda

This will spin down the specified disk (sda) after 30 minutes of inactivity. Here is a small explanation of the time paramter:

 -S:
Set the standby (spindown) timeout for the drive. This value is used by the drive to determine how long to wait  (with no disk activity)  before turning off the spindle motor to save power. Under such circumstances, the drive may take as long as 30 seconds to respond to a subsequent disk access, though most drives are much quicker. The encoding of the timeout value is somewhat peculiar.  A value of zero means "timeouts are disabled": the device will not automatically enter standby mode.  Values from 1 to 240 specify multiples of 5 seconds, yielding timeouts from 5 seconds to 20 minutes. Values from 241 to 251 specify from 1 to 11 units of 30 minutes, yielding timeouts from 30 minutes to 5.5 hours. A value of 252 signifies a timeout of 21 minutes. A value of 253 sets a vendor-defined timeout period between 8 and 12 hours, and the value 254 is reserved.  255 is interpreted as 21 minutes plus 15 seconds. Note that some older drives may have very different interpretations of these values.

Don't forget to reboot after you entered the command.