|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Reading and Writing Files
By: FYICenter.com
Part:
1
2
3
4
(Continued from previous part...)
How To Write a String to a File without a File Handle?
If you have a string, want to write it to a file, and you don't want to open
the file with a file handle, you can use the file_put_contents(). It opens the specified file,
writes the specified string, closes the file, and returns the number of bytes written.
Here is a PHP script example on how to use file_put_contents():
<?php
$string = "Download PHP scripts at dev.fyicenter.com.\r\n";
$string .= "Download Perl scripts at dev.fyicenter.com.\r\n";
$bytes = file_put_contents("/temp/todo.txt", $string);
print("Number of bytes written: $bytes\n");
?>
This script will print:
Number of bytes written: 89
If you look at the file todo.txt, it will contain:
Download PHP scripts at dev.fyicenter.com.
Download Perl scripts at dev.fyicenter.com.
How To Write an Array to a File without a File Handle?
If you have an array, want to write it to a file, and you don't want to open
the file with a file handle, you can use the file_put_contents(). It opens the specified file,
writes all values from the specified string, closes the file, and returns the number of bytes written.
Here is a PHP script example on how to use file_put_contents():
<?php
$array["one"] = "Download PHP scripts at dev.fyicenter.com.\r\n";
$array["two"] = "Download Perl scripts at dev.fyicenter.com.\r\n";
$bytes = file_put_contents("/temp/todo.txt", $array);
print("Number of bytes written: $bytes\n");
?>
This script will print:
Number of bytes written: 89
If you look at the file todo.txt, it will contain:
Download PHP scripts at dev.fyicenter.com.
Download Perl scripts at dev.fyicenter.com.
How To Read Data from Keyborad (Standard Input)?
If you want to read data from the standard input, usually the keyboard,
you can use the fopen("php://stdin") function. It creates a special file handle
linking to the standard input, and returns the file handle. Once the standard input
is opened to a file handle, you can use fgets() to read one line at a time from
the standard input like a regular file. Remember fgets() also includes "\n" at the end of the returning string.
Here is a PHP script example on how to read from standard input:
<?php
$stdin = fopen("php://stdin", "r");
print("What's your name?\n");
$name = fgets($stdin);
print("Hello $name!\n");
fclose($stdin);
?>
This script will print:
What's your name?
Leo
Hello Leo
!
"!" is showing on the next line, because $name includes "\n" returned by fgets().
You can use rtrim() to remove "\n".
If you are using your script in a Web page, there is no standard input.
If you don't want to open the standard input as a file handle yourself,
you can use the constant STDIN predefined by PHP as the file handle for standard input.
How To Open Standard Output as a File Handle?
If you want to open the standard output as a file handle yourself,
you can use the fopen("php://stdout") function. It creates a special file handle
linking to the standard output, and returns the file handle. Once the standard output
is opened to a file handle, you can use fwrite() to write data to the starndard output
like a regular file.
Here is a PHP script example on how to write to standard output:
<?php
$stdout = fopen("php://stdout", "w");
fwrite($stdout,"To do:\n");
fwrite($stdout,"Looking for PHP hosting provider!\n");
fclose($stdout);
?>
This script will print:
What's your name?
To do:
Looking for PHP hosting provider!
If you don't want to open the standard output as a file handle yourself,
you can use the constant STDOUT predefined by PHP as the file handle for standard output.
If you are using your script in a Web page, standard output is merged into the Web page HTML document.
print() and echo() also writes to standard output.
Part:
1
2
3
4
|