Page 1 of 1

Opening a file for reading

New postPosted: Sat Dec 28, 2013 1:38 pm
by Cat
Example 1:

# Opening a file for reading.

$file = file("file.txt");
$j = count($file);
for($i=0; $i<$j; $i++) {
echo "$file[$i]<br>";

Example 2:

# Opening a file for reading.

$my_file = "file.txt";
$fp = fopen($my_file, 'r');
$buffer = fread($fp, filesize($my_file));
echo $buffer;

Example 3:

# Opening a file for writing.

$my_file = "/path/to/file.txt";
$message = "This is a line of text"."\n";
$fp = fopen ($my_file, 'w');
fwrite ($fp, $message);
fclose ($fp); }

Example 4:

# Opening a file for appending.

$my_file = "/path/to/file.txt";
$message = "This is a line of text"."\n";
$fp = fopen ($my_file, 'a');
fwrite ($fp, $message);
fclose ($fp);

# PHP Filesystem functions.

int <? phpdoc("copy"); ?>(string source_file, string destination_file)
Copy a file

int <? phpdoc("fclose"); ?>(int fp)
Close an open file pointer

int <? phpdoc("feof"); ?>(int fp)
Test for end-of-file on a file pointer

int <? phpdoc("fflush"); ?>(int fp)
Flushes output

string <? phpdoc("fgetc"); ?>(int fp)
Get a character from file pointer

array <? phpdoc("fgetcsv"); ?>(int fp, int length)
Get line from file pointer and parse for CSV fields

string <? phpdoc("fgets"); ?>(int fp, int length)
Get a line from file pointer

string <? phpdoc("fgetss"); ?>(int fp, int length [, string allowable_tags])
Get a line from file pointer and strip HTML tags

array <? phpdoc("file"); ?>(string filename [, int use_include_path])
Read entire file into an array

bool <? phpdoc("flock"); ?>(int fp, int operation [, int wouldblock])
Portable file locking

int <? phpdoc("fopen"); ?>(string filename, string mode [, int use_include_path])
Open a file or a URL and return a file pointer

int <? phpdoc("fpassthru"); ?>(int fp)
Output all remaining data from a file pointer

int <? phpdoc("fread"); ?>(int fp, int length)
Binary-safe file read

mixed <? phpdoc("fscanf"); ?>(string str, string format [, string ...])
Implements a mostly ANSI compatible fscanf()

int <? phpdoc("fseek"); ?>(int fp, int offset [, int whence])
Seek on a file pointer

int <? phpdoc("fstat"); ?>(int fp)
Stat() on a filehandle

int <? phpdoc("ftell"); ?>(int fp)
Get file pointer's read/write position

int <? phpdoc("ftruncate"); ?>(int fp, int size)
Truncate file to 'size' length

int <? phpdoc("fwrite"); ?>(int fp, string str [, int length])
Binary-safe file write

array <? phpdoc("get_meta_tags"); ?>(string filename [, int use_include_path])
Extracts all meta tag content attributes from a file and returns an array

int <? phpdoc("mkdir"); ?>(string pathname, int mode)
Create a directory

int <? phpdoc("pclose"); ?>(int fp)
Close a file pointer opened by popen()

int <? phpdoc("popen"); ?>(string command, string mode)
Execute a command and open either a read or a write pipe to it

int <? phpdoc("readfile"); ?>(string filename [, int use_include_path])
Output a file or a URL

string <? phpdoc("realpath"); ?>(string path)
Return the resolved path

int <? phpdoc("rename"); ?>(string old_name, string new_name)
Rename a file

int <? phpdoc("rewind"); ?>(int fp)
Rewind the position of a file pointer

int <? phpdoc("rmdir"); ?>(string dirname)
Remove a directory

int <? phpdoc("set_file_buffer"); ?>(int fp, int buffer)
Set file write buffer

int <? phpdoc("set_socket_blocking"); ?>(int socket_descriptor, int mode)
Set blocking/non-blocking mode on a socket

array <? phpdoc("socket_get_status"); ?>(resource socket_descriptor)
Return an array describing socket status

bool <? phpdoc("socket_set_timeout"); ?>(int socket_descriptor, int seconds, int microseconds)
Set timeout on socket read to seconds + microseonds

string <? phpdoc("tempnam"); ?>(string dir, string prefix)
Create a unique filename in a directory

int <? phpdoc("tmpfile"); ?>(void)
Create a temporary file that will be deleted automatically after use

int <? phpdoc("umask"); ?>([int mask])
Return or change the umask

int <? phpdoc("unlink"); ?>(string filename)
Delete a file

Anyone who wishes to make additions or changes to this tutorial email them to