Home | MySQL Russian Manual | MySQL Manual | Apache HTTP Server Rus Documentation | Apache HTTP Server Documentation | downloads | faq

search for in the  Language: Russian


(PHP 4, PHP 5)

fputsAlias of fwrite()


This function is an alias of: fwrite().

User Contributed Notes
ontheroadagain4 at excite dot com
27-Jun-2009 11:33
Building on camillord's example above, a little tool I produced to get an idea which pages are most popularly being viewed. Adds to an 'activity.log'. Warning! - involves British Summer Time offset, obviously not applicable if you don't live in Britain LOL

// British Summer Daylight Saving Time! true = use dst ; false = don't. < This also based on another example elsewhere (see date())
$dst = true;
//   $dst = false;

$datetime = strtotime("now");
   if (
$dst && date('I')) $datetime += 3600; // +1hr offset

$txUser = $_COOKIE["txName"] ? $_COOKIE["txName"] : "Unknown";

$fileContainer = "activity.log";

$filePointer = fopen($fileContainer, "a");

$logMsg = $txUser . ", " . $_SERVER["REMOTE_ADDR"] . "; " . gmdate("r", $datetime) . " " . $_SERVER["PHP_SELF"] . "\n";

fputs($filePointer, $logMsg);


Result is a growing list of visitors in the form e.g.:

Fred Blogs, 12.345.678.901; Fri, 26 Jun 2009 10:59:16 +0000 /testpage3.php

Probably not a good idea if you have lots of hits as it can get quite big quite quickly. The name (if exists) comes from a saved cookie where user has used a contact form for instance.
camilord at kagayan dot com
13-Feb-2009 02:47
Here's a sample using fputs() function... it may be useful like making logs visiting your website by recording the IP address.


// file container where all texts are to be written
$fileContainer = date("MjY").'.log';

// open the said file
$filePointer = fopen($fileContainer,"w+");

// text to be written in the file
$logMsg = "You are located at ".$_SERVER["REMOTE_ADDR"]."\n";

// below is where the log message has been written to a file.

// close the open said file after writing the text


that's it... :)

-- camilord
stalker at ruun dot de
14-Sep-2005 07:08
as a continue of rembert:
so you should always use strlen($cmd) as third parameter if you use fsockopen -- because nearly everything you write e.g. in telnet or ssh has 2 or more "words".

(proud user of php4)

gr__z stalker
rembert at floating-point dot nl
21-Nov-2002 12:03
Adding to Adam (Nedstat):
fputs without the length parm just writes all data up to but not including the first 0 byte it encounters.

Therefore fputs($fp,"hello\0world") will only write hello to $fp whereas
fputs($fp,"hello\0world",11) will write both words.

If this doesn't make sense for you, remember strings are always terminated with that 0 byte. Binary data doesn't have such a terminator byte as a 0 byte can be a completely valid piece of data therefore you always need to know the length of the binary datapart.

BTW strlen() is binary safe, so you can use that to get the length of the binary data part as well - this is different from C which counts the number of chars up to the 0 byte. So the example above could also be written as:

yvan dot rivard at cesart dot com
17-Apr-2001 09:52
This might seem silly for experienced users, but this bugged me for about two hours (searching and trying to debug the damn thing).

If you're on a Windows system doing tests on a Linux/Apache setup, and you're writing stuff to a text file (then load that file to see if your content is being written properly) and you realize only your first string is in there, your problem is not your Windows, not Linux and not Apache. It's probably Samba (the thing that makes it possible for your Linux and Windows boxes to talk to each other) that's caching your file... Try viewing your file via telnet or simply copy your file then read that copy.

You can see content is being written to your file anyway, because even though you don't see your new content (using notepad for example), you do see your filesize increasing as you add more text to your file.
08-Oct-2000 07:07
You can use here-documents in PHP but not for printing to a file pointer.

There's a lot to say on them so just go to the page: http://www.php.net/manual/language.types.string.php
Doug at Here dot Com
01-Jul-2000 12:46
The way I understand fputs (which is purported as an alias to fwrite which can be "Binary Safe")...

= fopen("something.txt","w");
$string = "Hello World\\n" // escape the slash from being magically
// being transformed into a newline
fwrite($fp, $string); // will proccess /n as newline ...
fwrite($fp, $string, strlen($string)); // will write the entire string to file without changing the '/n'
// into a single byte for newline on Unix-like machines or CR/LF on Win32 machines

Hope this helps explain the definition of "could be Binary Safe". This is the reason why you must specify the length!

adam at nedstat dot nl
16-Jun-1999 03:19
A note about including length: if you are writing binary data you should *always* specify the data's length.  The reason is that the operating system has a special character to denote end of *strings*, but not for binary data.  Binary data can be anything, so you can't very well reserve an end-of-data character.  So the reason why writing binary data without specifying it's length can truncate it is that the only thing the system has to go on (without writing the entire contents of memory starting at the variable's address) is the end-of-string character, which could very well appear randomly in the middle of your set of binary data.  The reason that length() could have worked on the variable is that it is implemented in C as sizeof(), which actually fetches the size of the memory chunk associated with the variable, but this is not advisable because sizeof() can also return the size of the *pointer* to the variable if you're not careful (ie, passing it by reference into a second function).  In C it's best to keep track of size of the data you are accumulating as you accumlate it, so probably in PHP, too.
bschuetz at affinity dot net
23-Dec-1998 03:30
Length is required if you are writing out a lot of data.  For instance, if you are base64 decoding an email attachment and writing it out to a file you have to specify the length if the file is over a certian size or else the binary data will be corrupt.


Will yield the desired results.

When tested with a 1k the length wasn't required but with a larger file, around 27k it needs the length to work properly.

credits | contact