PHP fopen() Function

In this chapter you will learn:

  1. Definition for PHP fopen() Function
  2. Syntax for PHP fopen() Function
  3. Parameter for PHP fopen() Function
  4. Return for PHP fopen() Function
  5. Example - Output error message if cannot open a file
  6. Example - Open Remote Files

Definition

The fopen() function opens a file or URL.

fopen() is lifted straight from C,

Syntax

PHP fopen() Function has the following syntax.

fopen(filename,mode,include_path,context)

Parameter

ParameterIs requiredDescription
filenameRequired.File or URL to open
modeRequired.Type of access you require to the file/stream.
include_pathOptional.Set to '1' to search for the file in the include_path in php.ini
contextOptional.Context of the file handle.

Possible values for mode:

ModeMeaningNote
"r"Read only.Starts at the beginning of the file
"r+"Read/Write.Starts at the beginning of the file
"w"Write only.Opens and clears the contents of file; or creates a new file if it doesn't exist
"w+"Read/Write.Opens and clears the contents of file; or creates a new file if it doesn't exist
"a"Write only.Opens and writes to the end of the file or creates a new file if it doesn't exist
"a+"Read/Write.Preserves file content by writing to the end of the file
"x"Write only.Creates a new file. Returns FALSE and an error if file already exists
"x+"Read/Write.Creates a new file. Returns FALSE and an error if file already exists

Return

Returns a file pointer resource on success, or FALSE on error.

We can hide the error output by adding an '@' in front of the function name.

Example 1

Take a look at the following usages:


$file1 = fopen("file.txt", "r") OR die ("Can't open file!\n");
$file2= fopen("$logFileName", "w") OR die ("Log file not writeable!\n");

The fopen() function returns a file handle resource. You should store the return value of fopen() in a variable for later use:


<?PHP/*from   j a v a 2  s. c  o m*/
      $filename = "c:/abc/test.txt";
      $handle = fopen($filename, "a");
      if (!$handle) {
             print "Failed to open $filename for appending.\n";
      }
?>

Example 2

The fopen() can specify remote files. PHP automatically opens a HTTP/FTP connection for you, returning the file handle.

This example displays the google web site through your browser:


<?PHP// j a  v a  2s . c  o  m
$google = fopen("http://www.google.org", "r");
$site = fread($google, 200000);
fclose($google);
print $site;
?>

The r mode is specified because web servers do not allow writing through HTTP,


$file = fopen("http://www.example.com/","r");
$file = fopen("ftp://user:password@example.com/test.txt","w");

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP fpassthru() Function
  2. Syntax for PHP fpassthru() Function
  3. Parameter for PHP fpassthru() Function
  4. Return for PHP fpassthru() Function
  5. Note on PHP fpassthru() Function
  6. Example - Send rest of the file to the output buffer
  7. Example - Dump index page of a www server
Home » PHP Tutorial » PHP File Functions
PHP basename() function
PHP chdir() function
PHP chgrp() Function
PHP chmod() Function
PHP chown() Function
PHP chroot() Function
PHP clearstatcache() Function
PHP closedir() Function
PHP copy() Function
PHP delete() function
PHP dir() Function
PHP dirname() Function
PHP disk_free_space() Function
PHP disk_total_space() Function
PHP diskfreespace() Function
PHP fclose() Function
PHP feof() Function
PHP fflush() Function
PHP fgetc() Function
PHP fgets() Function
PHP fgetss() Function
PHP file() Function
PHP file_exists() Function
PHP file_get_contents() Function
PHP file_put_contents() Function
PHP fileatime() Function
PHP filectime() Function
PHP filegroup() Function
PHP fileinode() Function
PHP filemtime() Function
PHP fileowner() Function
PHP fileperms() Function
PHP filesize() Function
PHP filetype() Function
PHP flock() Function
PHP fnmatch() Function
PHP fopen() Function
PHP fpassthru() Function
PHP fprintf() Function
PHP fputcsv() Function
PHP fputs() Function
PHP fread() Function
PHP fscanf() Function
PHP fseek() Function
PHP fstat() Function
PHP ftell() Function
PHP ftruncate() Function
PHP fwrite() Function
PHP getcwd() function
PHP glob() Function
PHP is_dir() Function
PHP is_executable() Function
PHP is_file() Function
PHP is_link() Function
PHP is_readable() Function
PHP is_uploaded_file() Function
PHP is_writable() Function
PHP link() Function
PHP linkinfo() Function
PHP lstat() Function
PHP md5_file() Function
PHP mkdir() Function
PHP move_uploaded_file() Function
PHP opendir() Function
PHP parse_ini_file() Function
PHP pathinfo() Function
PHP pclose() Function
PHP popen() Function
PHP readdir() Function
PHP readfile() Function
PHP readlink() Function
PHP realpath() Function
PHP rename() Function
PHP rewind() Function
PHP rewinddir() Function
PHP rmdir() Function
PHP scandir() Function
PHP set_file_buffer() Function
PHP sha1_file() Function
PHP stat() Function
PHP symlink() Function
PHP tempnam() Function
PHP tmpfile() Function
PHP touch() Function
PHP umask() Function
PHP unlink() Function
PHP vfprintf() Function