★ wanayoo — archive 1999 http://php.net/manual/ja/function.fopen.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to ファイルシステム
Quick Reference
ファイルシステム
English version of this pageGerman version of this pageJapanese version of this page
Italian version of this pageFrench version of this pageHungarian version of this page
Spanish version of this page
* basename
* chgrp
* chmod
* chown
* clearstatcache
* copy
* delete
* dirname
* diskfreespace
* fclose
* feof
* fgetc
* fgetcsv
* fgets
* fgetss
* file
* file_exists
* fileatime
* filectime
* filegroup
* fileinode
* filemtime
* fileowner
* fileperms
* filesize
* filetype
* flock
* fopen
* fpassthru
* fputs
* fread
* fscanf
* fseek
* fstat
* ftell
* ftruncate
* fwrite
* set_file_buffer
* is_dir
* is_executable
* is_file
* is_link
* is_readable
* is_writeable
* is_uploaded_file
* link
* linkinfo
* mkdir
* move_uploaded_file
* pclose
* popen
* readfile
* readlink
* rename
* rewind
* rmdir
* stat
* lstat
* realpath
* symlink
* tempnam
* tmpfile
* touch
* umask
* unlink

  Thanks to:
 Chek.com
 easyDNS
 VA Linux Systems

  Related sites:
Apache
MySQL
PostgreSQL
Zend Tech.

  Community:
LinuxFund.org
OSDN
Manual: fopen
View the source code for this pageSearch the site



Previous page
 flock
 Updated
Fri, 15 Sep 2000
fpassthru 
Next page


fopen

(PHP3 <= 3.0.16, PHP4 )

fopen -- ファイルまたはURLをオープンする

説明

int fopen (string filename, string mode [, int use_include_path])

filenameが"http://"(大文字小文字は区別しません) で始まっている場合、指定されたサーバに対するHTTP 1.0コネクションが オープンされ、その応答テキストの先頭を指しているファイルポインタが 返されます。

HTTPリダイレクトは処理されないので、ディレクトリを指壙る場合は 末尾にスラッシュを付けなければなりません。

filenameが"ftp://"(大文字小文字は区別しません) で始まっている場合、指定されたサーバに対する FTPコネクションがオープンされ、要求したファイルへのポインタが 返されます。そのサーバがパッシブ・モードFTPをサポートして いない場合は、この関数は失敗します。 ftp経由で読みこみまたは書きこみのどちらかでファイルオープンする ことが可能です、(しかし、同時に両方を行うことはできません。)

filenameが"php://stdin","php://stdout", "php://stderr"のどれかで始まっている場合、対応するstdioストリームが オープンされます。 (この機能はPHP 3.0.13で導入されました。これ以前のバージョンでは、 stdioストリームにアクセスするために "/dev/stdin"または"/dev/fd/0"のようなファイル名を使用する必要があります。)

filenameが上記以外の場合、指定された ファイルがファイルシステムよりオープンされ、そのオープンされた ファイルへのファイルポインタが返されます。

オープンに失敗すると、この関数はfalseを返します。

modeは次のどれかになります。

  • 'r' - 読みこみのみでオープンします。ファイルポインタをファイルの 先頭に置きます。

  • 'r+' - 読みこみ・書きこみ用にオüプンします。ファイルポインタを ファイルの先頭に置きます。

  • 'w' - 書きこみの?gオープンします。ファイルポインタをファイルの 先頭に置き、ファイルサイズをゼロにします。ファイルが存在しない 場合には、作成を試みます。

  • 'w+' - 読みこみ・書きこみ用でオープンします。ファイルポインタを ファイルの先頭に置き、ファイルサイズをゼロにします。 ファイルが存在しない場合には、作成を試みます。

  • 'a' - 書きこみのみでオープンします。ファイルポインタをファイルの 終端に置きます。ファイルが存在しない場合には、作成を試みます。

  • 'a+' - 読みこみ・書きこみ用でオープンします。ファイルポインタを ファイルの終端に置きます。ファイルが存在しない場合には、作成を 試みます。

mode は文字'b'を指定することもできます。 これはバイナリとテキストのファイル形式が異なるシステムでのみ有効です。 (つまり、UNIXでは不要です。)必要がない場合、無視されます。

オプションの2番目の引数を使用して、これに"1"を設定する ことにより、include_path のファイルの検索も行うこともできます。

例 1. fopen()の例


$fp = fopen ("/home/rasmus/file.txt", "r");
$fp = fopen ("/home/rasmus/file.gif", "wb");
$fp = fopen ("http://www.php.net/", "r");
$fp = fopen ("ftp://user:password@example.com/", "w");
      

ファイルの読みこみ・書きこみ時に問題が発生し、 サーバーモジュール版のPHPを使用している場合、 使用するファイル・ディレクトリがサーバープロセスからアクセス可能 かどうかを確認してみてください。

Windows 環境では、ファイルパスで用いる全てのバックスラッシュを エスケープするかフォワードスラッシュを使用することに注意して下さい。


$fp = fopen ("c:\\data\\info.txt", "r");
      

fclose(),fsockopen(), socket_set_timeout(),popen() も参照下さい。


User Contributed Notes: fopen


Espen.Lyngaas@colorline.no
25-Oct-1999 03:13
Reading a url with a username and password works through a proxy server too, but (as far as I can tell) you need to enable "basic authentication" on proxy servers that doesn't have this feature on by default (MS Proxy)

<PRE>$fp = fopen(proxyuser:proxypassword@www.somehost.com/index.html);
</PRE>

If you specify the wrong proxy userid/password or nothing at all, you will receive a "success" warning, which is a bit confusing, but obviously nothing from the url you requested.



barry@hostingmaster.net
30-Oct-1999 05:34
Hey beginners (from a beginner) if you find this confusing as I did, take a look at fread() where you will find both the reading of the information and the opening of the file to be read.
Barry.



icon@mricon.com
09-Nov-1999 02:44
If you're running PHP as apache module, it will always write files as "nobody", "www", "httpd", (or whatever user your webserver runs as) unless you specify a different user/group in httpd.conf, or compile apache with suexec support.
However, if you run PHP as a CGI wrapper, you may setuid the PHP executable to whatever user you wish (*severe* security issues apply). If you really want to be able to su to other user, I recommend compiling with suexec support.
AFAIK, PHP can't NOT use SuEXEC if apache does. If PHP is configured as an apache module it will act as whatever user the apache is. If apache SuEXEC's to otheruser:othergroup (e.g. root:root), that's what PHP will write files as, because it acts as a part of apache code. I suggest you double-check your SuEXEC configuration and settings. Note: you can't su to another user within the PHP code -- it has to be an apache directive, either through <VirtualHost>, or through .htaccess. Also note: I'm not sure how it all works (if it works at all) on Win32 platforms.
Check www.apache.org to see how it's done.



peksalli@iki.fi
08-Dec-1999 03:34
About writing to file with a specific user id:
You can usually change file permissions with FTP. Just open a FTP connection and say "chmod o+w filename". It is also possible to use FTP connection to write the file, for example, "fopen(ftp://user:password@ftpserver.address/file,w)". That way the file is created and used with your username and password, which (IMHO) is more secure than having files with write permission for all.



bmueller@creotech.com
18-Dec-1999 10:25
RE: suEXEC
According to the Apache Group PHP scripts do not follow suEXEC even if it's set in the <VirtualHost> Directive. I run a webserver and whenever a new user is added it automatically creates a <VirtualHost> entry for them and adds the USER and GROUP numbers for usage with suEXEC. Normal CGI scripts work perfectly with this setup however PHP3 scripts completely ignore the USER and GROUP values and continue to execute as nobody.

Brian Mueller



styx@xlarge.at
23-Dec-1999 10:08
re: file upload
http://www.php.net/manual/html/feature-fileupload.html



sgarner@expio.co.nz
11-Jan-2000 12:28
To review@web.com:

filesize() does not support URLs, only local files. Replace "filesize($filename)" with some large number, e.g. "1000000".



sgarner@expio.co.nz
16-Jan-2000 12:42
You can use "@" signs in passwords if you first run the password through rawurlencode() to convert the @ sign to its URL encoded equivalent.

$passcoded = rawurlencode($pass);
$outputfile = fopen("ftp://someuser:$passcoded@someplace.net/some/directory/to/file.txt","w");



jamie.watt@murchison.com.au
02-Feb-2000 07:39
To check if a file exists using http or ftp use the following:

<pre>
$fp = @fopen("http://www.someurl.com/testfile.php3","r");
if ($fp)
{ print"The file exists!"; }
else
{ print"The file does not exist"; }
</pre>

Note: The "@" in front of fopen suppresses the error output of the function.

I hope this clears up some confusion.



alex@dentonlive.com
28-Feb-2000 04:21
Re: suEXEC
I have a work around for the file ownership problem.
First do a chown with a perl script to the user right after the file is created.
But keep the gid to the httpd (nobody).
The web interface can still update the file depending on the user wants, and a user gets access to the file.
Beware with the script, it may lead to a security problem.
The best solution in PHP is to fix the script to run as a user rather than httpd in virtual directory.



malhavoc@stomped.com
07-Jul-2000 05:48
Just a note to individuals who use the fopen() function to reference an external url. fopen() makes no attempt to url encode the url before opening it, so be sure to do that first.

It would be a nice touch to make this function change spaces to + signs and remove illegal url characters, but you can easily get around this by making use of the urlencode() function first.

Enjoy.



WE@MYOIL.DE
11-Jul-2000 07:54
PHP3 and fopen via proxy-
here is the solution:
....
$myfiles = "http://www.xyz.com/cgi-bin/plz_suche/search_or.cgi?or_plz=$PLZ&or_ort=".rawurlencode($stadt);

$datei = fsockopen("proxy.wdf.sap-ag.de", 8080, &$errno, &$errstr);
if( !$datei )
{
echo "proxy not available !";
fclose($resultfile);
exit();
} else {
fputs($datei,"GET $myfiles/ HTTP/1.0\n\n");
while (!feof($datei))
{
$zeile = fgets($datei,1000);
}
}

Best regards,

Wilfried Ehrensperger



kstone@trivergent.net
19-Aug-2000 03:44
If you want to run php with suexec, make a interpreter out of it, and then put a cgi=0 in the source (cgi_main.c) or something like that. Grep for SERVER_NAME and you'll see it. Running php w/ binfmt_misc on linux is the way to go, and hopefully the php Gods will bless us with a version I won't have to patch!


heikki.korpela@wapit.com
26-Aug-2000 02:25
(Copy from my note on fread())

Indeed, the fopen-wrappers aren't written too well - they might easily force your server its knees if the server you're connecting to is encountering problems.

You could try the CURL support that is in the CVS version, or write your own HTTP implementation either within the fopen-wrappers yourself in C, or try using the fsockopen in PHP and do it within PHP with sockets. With fsockopen, you would connect to port 80 (or whichever is running the WWW server) in the server you wish to request the page, send a 'GET /URI HTTP/1.[01]' with whatever headers you need, and then parse the headers and the content.



kessler@kessler-design.com
27-Sep-2000 08:47
Possible security-problem with fopen() if error-reporting is not suppressed.

Imagine you use 2 scripts on same or different hosts, but only first one is visible for the user. Within the first script, you construct an URL with some (maybe not public) parameters to call the second script, parse it's output and return something to the user, still from the first script.

#we are in script1.php and we construct the URL, then we try to open it
$filename="http://domain.com/script2.php?someparam=1&somemore=2";

$fh = fopen($filename, "r");
if ($fh) {
# ...
}

If the host (of second script) is not reachable, we wait a while and then get (with 'normal' error-reporting) the following text with the users browser-window.

Warning: fopen("http://domain.com/script2.php?someparam=1&somemore=2","r") - Unknown error in domain.com\script1.phtml on line xxx

The error-report shows the whole (not public) filename of the second script including all (not public) params instead of a simple connection timeout-message.

Make sure that you set error_reporting if you have to use such constructions.



 About Notes


Previous page
 flock
 Updated
Fri, 15 Sep 2000
fpassthru 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.