Jump to: navigation, search

Php fopen

From w3cyberlearnings

Contents

PHP function fopen

This function create a file handle for a file or URL.

Syntax fopen

  • name is a file name or URL string
  • mode: specifices how the fopen handle the access
  • include_path: when sets it to true, it searches for the file in the include_path (in php.inin)
  • context: sets this option can modify the behavior of a stream.
fopen(name,mode, include_path, context);

mode

  • r: read only (read starts at the very begining of the file)
  • r+: read and write (it starts at the very begining of a file)
  • w: write only (create and clear the content of a file before start to write a new content)
  • w+ read and write (create and clear the content of a file before start to write to a new content)
  • a: write only (create and write content to the end of the file)
  • a+ read and write (create and write content to the end of the file, but preserves file content)
  • x: write only (create a new file, and generate error when the file is already existed)
  • x+: read and write (create a new file, and generate error when the file is already existed)
  • c: write only (create a new file when it does not already exist.

Note

For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().


Example 1

<?php

$fh = fopen("/home/abc/file.txt", "r");
$fh = fopen("/home/abc/file.gif", "wb");
$fh = fopen("http://www.google.com/", "r");
$fh = fopen("ftp://user:[email protected]/somefile.txt", "w");
?>

Example 2

$fh = fopen ("http://www.some.com/index.html", "r");
while (!feof($fh)) {
   echo fgets($fh,4096);
}

Related Links


basename-- chgrp-- chmod-- chown-- clearstatcache-- copy-- delete-- dirname-- disk_free_space-- disk_total_space-- diskfreespace-- fclose-- feof-- fflush-- fgetc-- fgetcsv-- fgets-- fgetss-- file_exists-- file_get_contents-- file_put_contents- file-- fileatime-- filectime-- filegroup-- fileinode-- filemtime-- fileowner-- fileperms-- filesize-- filetype-- flock-- fnmatch-- fopen-- fpassthru-- fputcsv-- fputs-- fread-- fscanf-- fseek-- fstat-- ftell-- ftruncate-- fwrite-- glob-- is_dir-- is_executable-- is_file-- is_link-- is_readable-- is_uploaded_file-- is_writable-- is_writeable-- lchgrp-- lchown-- link-- linkinfo-- lstat-- mkdir-- move_uploaded_file-- parse_ini_file-- parse_ini_string-- pathinfo-- pclose-- popen-- readfile-- readlink-- realpath_cache_get-- realpath_cache_size-- realpath-- rename-- rewind-- rmdir-- set_file_buffer-- stat-- symlink-- tempnam-- tmpfile-- touch-- umask-- unlink--

Navigation
Web
SQL
MISC
References