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

PHP Home Page

Manual Table of Contents
Up to Filesystem
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Filesystem
* 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
* link
* linkinfo
* mkdir
* pclose
* popen
* readfile
* readlink
* rename
* rewind
* rmdir
* stat
* lstat
* realpath
* symlink
* tempnam
* touch
* umask
* unlink
Manual: fopen
View the source code for this pageSearch the site



Previous page
 flock
 Updated
Sat, 12 Aug 2000
fpassthru 
Next page


fopen

(PHP3 , PHP4 )

fopen -- Open file or URL

Description

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

If filename begins with "http://" (not case sensitive), an HTTP 1.0 connection is opened to the specified server and a file pointer is returned to the beginning of the text of the response.

Does not handle HTTP redirects, so you must include trailing slashes on directories.

If filename begins with "ftp://" (not case sensitive), an ftp connection to the specified server is opened and a pointer to the requested file is returned. If the server does not support passive mode ftp, this will fail. You can open files for either reading and writing via ftp (but not both simultaneously).

If filename is one of "php://stdin", "php://stdout", or "php://stderr", the corresponding stdio stream will be opened. (This was introduced in PHP 3.0.13; in earlier versions, a filename such as "/dev/stdin" or "/dev/fd/0" must be used to access the stdio streams.)

If filename begins with anything else, the file will be opened from the filesystem, and a file pointer to the file opened is returned.

If the open fails, the function returns false.

mode may be any of the following:

  • 'r' - Open for reading only; place the file pointer at the beginning of the file.

  • 'r+' - Open for reading and writing; place the file pointer at the beginning of the file.

  • 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

  • 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

  • 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

  • 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

As well, mode may contain the letter 'b'. This is useful only on systems which differentiate between binary and text files (i.e., it's useless on Unix). If not needed, this will be ignored.

You can use the optional third parameter and set it to "1", if you want to search for the file in the include_path, too.

Example 1. fopen() example


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

If you are experiencing problems with reading and writing to files and you're using the server module version of PHP, remember to make sure that the files and directories you're using are accessible to the server process.

On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.

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

See also fclose(), fsockopen(), and popen().


User Contributed Notes: fopen


chopin@csone.kaist.ac.kr
25-Aug-1999 04:12
file_exists() does not support URL open. use fopen() for checking file existance, for example,
<PRE>
if( !($fp = fopen("http://csone.kaist.ac.kr/~chopin/", "r")) )
echo "URL open error\n";
else
echo "URL exists\n"
</PRE>



bertil.friman@fdk.se
30-Aug-1999 04:34

About fopen (... "w") (for beginners, by a beginner):
Creating files with fopen(... "w") does not work unless the http-
server has write permission to the directory where the file is to be located.
You can give the http-server this permission with the command:
"chmod o+w mydir/" (assuming your directory is called mydir/). The
command gives everybody permission to add and remove files in the
directory. When a file is created by php (fopen), php will have no problems
to later open it for write.

If you create the file yourself, and want to let php (as http-server)
open it for write with fopen(... "w"), then you must first give the
http-server permission to open the file for write. This can be done with the
command: "chmod o+w myfile" (assuming your file is called myfile).
The command gives everybody permission to write to the file.

Both these examples assume a Unix server (for example Linux) and
that you have access to the server either directly or through telnet.
If you only have FTP access to the server, I really do not know
if it is possible to use PHP to write into files.

Is there a better way to do this, anybody?



ac@artcenter.ac
21-Oct-1999 06:24
Fopen really really needs a time out option. I am building a spider that checks for URL validation.. well it works great except that sometimes when the URL is far away from the server it goes to the url but it goes EXTREMLY slowly and many times makes scripts hang for a long time before opening a different url. very uncomfortable. This command MUST have time out please !@


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. (ps. since this manuel is for idiots like me, it should include statements like this for someone to see an example ... don't get me wrong, I think PHP is the best thing since french toast and has opened a door for me into the world wide of the web! Barry.


dniq@kraft-s.ru
04-Nov-1999 02:57
That is strange, but fopen("file","w+") would create file as "nobody" or whatever your http server runs as. Why don't php create file with owner of the script???? It's a pity... :(


icon@mricon.com
09-Nov-1999 02:44
If you're running PHP as apache module, it will always write files as "nobody", 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. Check www.apache.org to see how it's done.


dniq@kraft-s.ru
11-Nov-1999 12:37
I ALREADY have Apache with SuEXEC, but PHP doesn't use it :(


elanor@universalmail.com
17-Nov-1999 02:45
I had an code to open remote URL by

fopen("$url","r");
It worked before. But it does not work now! It gives error msg:

Warning: fopen("http://www.metu.edu.tr/","r") - Transport endpoint is not connected in /home/sait/public_html/TEST/test.php3 on line 42


When I changed hostname with IP, it works. When I tried to change hostname to the IP by using gethostbyname, it does not works!!! it gives itself as result. The problem is in version PHP 3.0 OR I need to talk with system admin, for posiable DNS problems??



angus@europeonline.net
18-Nov-1999 04:54
The "Transport endpoint is not connected" error could be due to an incorrect entry in the /etc/hosts file. If you have configured two IP addresses for your server (for example to use SSL) these two IP addresses should not be mapped to the same name in the hosts file.


icon@mricon.com
07-Dec-1999 03:28
(about SuEXEC above) 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.


peksalli@iki.fi
08-Dec-1999 03:34
About writing to file (to Bertil Friman): You can usually change file permissions with FTP, too. 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.


zbir@burningchrome.com
15-Dec-1999 06:22
Seems that you can't use '@' characters in passwords in fopen - not even escaped as '\@' - that, or I'm doing this all wrong:

<pre>
$outputfile = fopen("ftp://someuser:p\@ssword@someplace.net/some/directory/to/file.txt","w");
fpassthru($outputfile);
fwrite($outputfile,$outputmessage);
</pre>



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. Is there ANY way what-so-ever to execute PHP with the UID of the account it is running from?

Thanks in advance,

Brian Mueller



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



php3@webgrafiken.de
03-Jan-2000 10:09
fopen seems to have a hang somehow. Calling fopen on existing files works fine for me on my local WAMP config, as well as ony my 3.0.12 solaris server; but opening a non-existing file with http gives a PARSE error - "no error in..." and for ftp "unknown error....".
It really should not produce a parse error if the function call fails!



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
zbir@burningchrome.com: You can use "@" signs in passwords if you first run the password through rawurlencode() to convert the @ sign to its URL encoded equivalent.


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

Get a work around for the file ownership problem.
First do a chown with the perl scripe 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 depend on the user want and user get the most access to the file.
Beware with the scripe, it may lead to a insecure problem.
The best solution is PHP fix the scripe run as user rather then httpd in virtual directory.



ian@oven.com
07-Mar-2000 04:11
The manual says that fopen makes an HTTP/1.0 connection to the server.

If you are using "Name Based Virtual Hosts", fopen will not work.

You will get an error like "Transport endpoint not connected" error.

Name based virtual hosts require HTTP/1.1.



willo@junkmail.co.za
01-Jul-2000 01:07
When writing BINARY files, such as images, use "wb".


mircho@abv.bg
06-Jul-2000 09:39
Either I am not geting it right, or it doesn't work, But I can't get a hnadle on <b>STDOUT</b>. I used the "php://stdout" but only got an server error <pre>Premature end of script headers</pre>.
It might be something in the new PHP4 functionality to buffer the entire content before sending it. Can anyone at least supply enough info on how to get to control the <b>stdout</b>



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.



mauba@psi.com.br
11-Jul-2000 01:15
feof() and URL's:
<pre>

$fp = fopen("$url","r");
while(!eof($fp)) {
do something;
}

</pre>
Do not work for me (the script seems to not finish the connection until the server don't); I had to write like this:
<pre>

$fp = fopen("$url","r");
while($line = fgets($fp,2048)) {
do something;
}
</pre>



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



dont spam: phobo@paradise.net.nz
12-Jul-2000 07:58
If you are running PHP Server on Windows NT/2000 (and I assume 95/98 too), remember you MUST use "rb" or "wb" when opening NON TEXT files, otherwise you'll be stumped like I was, wondering why your script only appears to partially process a file, not realising binary opening aint implied like it is on other platforms.


Rdrpixel@swbell.net
14-Jul-2000 06:37
Thanks for the 'rb' tip. I was up all night trying to figure that out.. Can someone PLEASE make a note in the docs about this where it's easier to find?


function@worldonline.dk
06-Aug-2000 10:46
Respons to : 27-Jul-2000 09:42
To remove warnings/errors returned by php simply put a @ in front of the funtion name. Like @fopen.



 About Notes


Previous page
 flock
 Updated
Sat, 12 Aug 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.