PHP/File Directory/fopen — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 07:04, 26 мая 2010
Содержание
- 1 Acceptable fopen() Modes
- 2 Calling fopen() with a Context Resource
- 3 File open with exception checking
- 4 fopen() function opens a file, returns an integer, as a file handle.
- 5 fopen() requires the file path and the mode in which the file is to be opened.
- 6 fopen() returns false if the file cannot be opened for any reason.
- 7 Getting and Printing a Web Page with fopen()
- 8 Getting and Putting Files with FTP
- 9 If you are writing a binary file on a Windows system, you should add a "b" flag.
- 10 Load remote file
- 11 Open a connection with the PHP site (http://www.php.net):
- 12 Opening a file
- 13 Opening a file on Windows
- 14 Opening a remote file
- 15 Opening Files
- 16 Opening files in the include_path
- 17 PHP 5 Arguments for Opening a File
- 18 Reading a File with fread()
- 19 Reading from standard input
- 20 Safely reading a binary file
- 21 Using fopen( )
- 22 Using the fopen() Function
Acceptable fopen() Modes
r Open the file for reading.
r+ Open the file for reading and writing.
w Open the file for writing, overwriting existing files, and creating the file if it does not exist.
w+ Open the file for reading and writing, overwriting existing files, and creating the file if it does not exist.
a Open the file for writing, creating the file if it does not exist, and appending to the file if it does.
a+ Open the file for reading and writing, creating the file if it does not exist, and appending to the file if it does.
b Open the file in binary reading/writing mode (applicable only on Windows systems; however, recommended in all scripts).
Calling fopen() with a Context Resource
<?php
$url = "http://www.wbex.ru/index.htm";
$options = array ("http" => array ("user_agent" => "php24-test-script", "header" => "referer: http://www.example.ru/index.html\r\n" ) );
$context = stream_context_create ( $options );
$res = fopen ( $url, "r", 0, $context ) or die ( "could not open page" );
while ( ! feof ( $res ) ) {
print fgets ( $res, 1024 );
}
?>
File open with exception checking
<?php
try {
$fh = fopen("data.txt", "r");
if (! $fh) {
throw new Exception("Could not open the file!");
}
}
catch (Exception $e) {
echo "Error (File: ".$e->getFile().", line ".
$e->getLine()."): ".$e->getMessage();
}
?>
fopen() function opens a file, returns an integer, as a file handle.
Its syntax is: int fopen (string file, string mode [, int use_include_path])
The mode specifies the read/write readiness of the file.
MODE MEANING
r Read only.
r+ Reading and writing.
w Write only.
w+ Reading and writing.
a Write only.
a+ Reading and writing.
If use_include_path is set to 1, file path is compared to the include path contained in the php.ini file.
<?
$file = "data.txt"; // some file
$fh = fopen($file, "a+") or die("File ($file) does not exist!");
?>
fopen() requires the file path and the mode in which the file is to be opened.
The most common modes are read ("r"), write ("w"), and append ("a").
fopen() returns a file resource you will later use to work with the open file.
To open a file for reading, you would use the following:
$fp = fopen( "test.txt", "r" );
You would use the following to open a file for writing:
$fp = fopen( "test.txt", "w" );
To open a file for appending, you would use this:
$fp = fopen( "test.txt", "a" );
fopen() returns false if the file cannot be opened for any reason.
if ( $fp = fopen( "test.txt", "w" ) ) {
// do something with $fp
}
Or you can use a logical operator to end execution if an essential file can"t be opened:
( $fp = fopen( "test.txt", "w" ) ) or die ("Couldn"t open file, sorry");
Getting and Printing a Web Page with fopen()
<html>
<head>
<title>Getting a Web Page with fopen()</title>
</head>
<body>
<div>
<?php
$webpage = "http://www.wbex.ru/index.htm";
$fp = fopen( $webpage, "r" ) or die("couldn"t open $webpage");
while ( ! feof( $fp )) {
print fgets( $fp, 1024 );
}
?>
</div>
</body>
</html>
Getting and Putting Files with FTP
<?php
$file_name = "somefile.ext";
$fp = fopen($file_name, "rb");
if ($fp) {
$data = fread($fp, filesize($file_name));
fclose($fp);
}
?>
If you are writing a binary file on a Windows system, you should add a "b" flag.
$fp = fopen( "binary_file", "wb" );
and read them like this:
$fp = fopen( "binary_file", "rb" );
Load remote file
<?php
$file_name = "http://php.net/index.php";
$fp = fopen ( $file_name, "r" );
if ($fp) {
$data = "";
while ( ! feof ( $fp ) ) {
$data .= fread ( $fp, 4096 );
}
fclose ( $fp );
}
echo $data;
?>
Open a connection with the PHP site (http://www.php.net):
<?
$site = "http://www.php.net"; // some server that can communicate via HTTP
$sh = fopen($site, "r"); // assigns PHP.net index page to a filehandle.
?>
Opening a file
<?php
$fh = fopen("file.txt","r") or die("can"t open file.txt: $php_errormsg");
?>
Opening a file on Windows
$fh = fopen("c:/settings.txt","rb");
Opening a remote file
<?php
$fh = fopen("http://www.example.ru/robots.txt","r") or die($php_errormsg);
?>
Opening Files
<?php
$file = "data.txt";
if (file_exists ($file)){
try {
if ($readfile = fopen ($file, "r")){
echo "File opened successfully.";
} else {
throw new exception ("Sorry, the file could not be opened.");
}
} catch (exception $e) {
echo $e->getmessage();
}
} else {
echo "File does not exist.";
}
?>
Opening files in the include_path
<?php
$fh = fopen("file.inc","r",true) or die("can"t open file.inc: $php_errormsg");
?>
PHP 5 Arguments for Opening a File
Argument Description
r Opens a file for reading
r+ Opens a file for both reading and writing
w Opens a file for writing only
w+ Opens a file for reading and writing
a Opens a file for appending (write-only)
a+ Opens a file for appending (read/write)
X Creates a file and opens it for writing only
x+ Creates a file and opens it for reading and writing
<?php
$file = "data.txt";
if (file_exists ($file)){
try {
if ($readfile = fopen ($file, "r")){
$curvalue = fread ($readfile,100);
echo $curvalue;
} else {
throw new exception ("the file could not be opened.");
}
} catch (exception $e) {
echo $e->getmessage();
}
} else {
echo "File does not exist.";
}
?>
Reading a File with fread()
<html>
<head>
<title>Reading a File with fread()</title>
</head>
<body>
<div>
<?php
$filename = "data.txt";
$fp = fopen( $filename, "r" ) or die("Couldn"t open $filename");
while ( ! feof( $fp ) ) {
$chunk = fread( $fp,16 );
print "$chunk<br/>";
}
?>
</div>
</body>
</html>
Reading from standard input
<?php
$fh = fopen("php://stdin","r") or die($php_errormsg);
while($s = fgets($fh)) {
print "You typed: $s";
}
?>
Safely reading a binary file
<?php
$fh = fopen("c:/images/logo.gif","rb");
?>
Using fopen( )
<?
$fp = fsockopen ("data.org", 80);
if ($fp) {
fwrite($fp, "GET / HTTP/1.1\r\nHOST: data.org\r\n
ACCEPT-ENCODING: gzip\r\n\r\n");
while (!feof($fp)) {
print fread($fp,256);
}
fclose ($fp);
} else {
print "Fatal error\n";
}
?>
Using the fopen() Function
<?php
$fr = fopen("myfile.txt", "r");
$fr = fopen("myfile.dat", "ba+");
$fr = fopen("code.php", "w+", true);
$fr = fopen("http://www.php.net/index.php", "r");
$fr = fopen("ftp://ftp.php.net/index.php", "r");
$url = "http://www.php.net/this is my invalid URL.php";
$url = urlencode($url);
$fr = fopen($url, "r");
?>