Saturday, January 31, 2009

File System Functions

basename --> Returns the filename from a full path.
Format: $filename = basename(“path”);
chdir --> Change to a different directory.
Format: chdir(“pathtodirectory”);
chgrp --> Changes the group for a file.
Format: chgrp(“pathtofile”,”group”);
chmod --> Changes the permissions of the file.
Format: chmod(“pathtofile”,”octalnumber”);
chown --> Changes the owner of a file.
Format: chown(“pathtofile”,”newowner”);
closedir --> Closes the directory pointed to by the directory handle $dh.
Format: closedir($dh);
copy --> Copies a file, resulting in two copies of the file.
Format: copy(“oldfilename”,”newfilename”);
dirname --> Returns the directory from a path.
Format: $directory_name = dirname(“path”);
dis_total_space --> Returns the number of bytes of total space on the disk.
Format: $space = disk_total_space(“path”);
disk_free_space --> Returns the number of bytes of free (unused) space.
Format: $free = disk_free_space(“pathtodir”);
fclose --> Closes an open file pointed to by the file handle $fh.
Format: fclose($fh);
feof --> Returns TRUE when the pointer reaches the end of the file.
Format: feof($fh);
fgetc
Returns one character (the current character) and moves the pointer to the next character in the file.
Format: $char = fgetc($fh);
fgetcsv
Reads a line of no longer than length from a file and returns it as an array, breaking it into elements at the specified separator, sep.
Format: $array_out = fgetcsv($fh,length,”sep”);
fgets, fgetss
Reads a line from file of no longer than length. Does not return the end-ofline character. fgetss also strips tags from line.
Format: $line = fgets($fh,length); $line=fgetss($fh,length);
file -->Reads a file and returns an array with one line per element.
Format: $array_lines = file($fh);
file_exists --> Checks whether a specific file exists. Returns TRUE or FALSE.
Format: $bool = file_exists(“pathtofile”);
fileatime --> Returns the time that the specified file was last accessed.
Format: $timestamp = fileatime(“pathtofilename”);
filectime --> Returns the time that the specified file was created.
Format: $timestamp = filectime(“pathtofilename”);
filemtime --> Returns the time that the specified file was last modified.
Format: $timestamp = filemtime(“pathtofile”);
fileowner --> Returns the user ID of the owner of the file.
Format: $userID = fileowner(“pathtofile”);
fileperms --> Returns the file permissions for the file.
Format: $perms = fileperms(“pathtofile”);
filesize --> Returns the file size in bytes.
Format: $size = filesize(“pathtofile”);
filetype --> Returns the file type, such a file or directory.
Format: $type = filetype(“pathtofile”);
flock --> Locks a file so that no one else can access it until it’s unlocked.
Format: flock($fh,mode);
fopen --> Opens a file and returns a pointer to the specified file.
Format: $fh = fopen(“pathtofile”,”mode”);
fputs
Writes text to a file. Alias for fwrite. Returns the number of bytes written to the file or FALSE if the function fails.
Format: $result = fputs($fh,”text”,length);
fread --> Reads number of bytes, unless it reaches the end of the file first, from a file.
Format: $file_content = fread($fh,number);
fscanf --> Reads text from a file and returns a formatted string.
Format: $string = fscanf($fh,”format”,$v1,$v2, . . .);
fseek
Moves the file pointer, depending on the next two parameters. The value specified in num is a number of characters. You can set mode to SEEK_SET (moves to char in position num), SEEK_CUR (moves num characters forward from current position), or SEEK_END (moves num characters back from the last character).
Format: fseek($fh,num,mode);
fwrite
Writes text to the file indicated by $fh, stopping at length. Specifying length is optional.
Format: $bytes_written = fputs($fh,”text”,length);
getcwd --> Returns the path to the current directory.
Format: $current_directory = getcwd();
getlastmod --> Returns the last modification date of the current script.
Format: $timestamp = getlastmod();
is_dir --> Checks whether the specified path is a directory.
Format: $bool = is_dir(“pathtodir”);
is_file --> Checks whether a specified file is a regular file, rather than a directory or special system file.
Format: $bool = is_file(“pathtofile”);
is_readable --> Checks whether a specified file is readable.
Format: $bool = is_readable(“pathtofile”);
is_uploaded_file --> Checks whether a specified file was uploaded via Web server form.
Format: $bool = is_uploaded_file(“pathtofile”);

is_writable --> Checks whether a specified file is writable.
Format: $bool = is_writable(“pathtofile”);
link --> Creates a hard link to path at newpath.
Format: link(“path”,”newpath”);
mkdir --> Creates a new directory. The value specified in mode is permissions in octal form.
Format: mkdir(“pathtonewdir”,mode);
move_uploaded_file --> Moves a file from its temporary upload directory to a permanent file.
Format: move_uploaded_file(“filename”,”pathtodestination”);
opendir --> Opens a directory. Returns a pointer to the open directory.
Format: $dh = opendir(“pathtodir”);
passthru --> Executes a system command and outputs the result.
Format: passthru(“systemcommand”);
pathinfo
Creates an array with information about a path. The array contains three elements: dirname, basename, and extension.
Format: $array_dir = pathinfo(“pathtodir”);
readdir --> Reads one filename from the open directory.
Format: $filename = readdir($dh);
readfile --> Reads a file and outputs the contents. Can handle a URL.
Format: $numberOfBytesRead = readfile(“pathtofile”);
rename --> Renames a file.
Format: rename(“oldfilename”,”newfilename”);
rewind --> Sets a file pointer to beginning of the file referred to by $fh.
Format: rewind($fh);
rmdir --> Removes a directory.
Format: rmdir(“pathtodir”);
tempnam --> Generates a unique filename with a specified prefix in the directory.
Format: $filename = tempnam(“pathtodir”,”prefix”);
tmpfile
Creates a temporary file with a unique name, opens it with write privileges, and returns a pointer to the open file.
Format: $fh = tmpfile();
touch
Sets the modification date of a file. If time isn’t specified, it sets the date to the current time. If the file does not exist, it’s created.
Format: $bool = touch(“pathtofile”,time);
umask
Sets the default permissions to mask and returns the previous mask. The previous defaults are restored at the end of the script.
Format: $old_mask = umask(mask);
unlink --> Deletes a file.
Format: unlink(“pathtofile”);

No comments:

Post a Comment