Assignment#04
Submitted by: Hassaan Shahid
Reg No: SP23-BSE-057
Course: Web technologies
Submitted To: Sir Umer Shahzad
1. basename()
Extracts the final component of a file path, removing any preceding directory parts.
Example:
<?
$path = "/var/www/html/index.";
echo basename($path);
?>
2. chmod()
Modifies the permission settings (mode) of a file or folder using the provided octal number.
Example:
<?
$file = __DIR__ . "/[Link]";
file_put_contents($file, "hello");
chmod($file, 0644);
?>
3. chown()
Updates the owner (and optionally the group) of a file or directory; needs elevated user rights to execute.
Example:
<?
$file = __DIR__ . "/[Link]";
chown($file, "www-data");
?>
4. Copy()
Makes an identical copy of the source file at the specified destination location.
Example:
<?
("[Link]", "backup/[Link]");
?>
5. unlink()
Deletes a file by removing its entry from the directory.
Example:
<?
$temp = __DIR__ . "/[Link]";
file_put_contents($temp, "");
unlink($temp);
?>
6. fopen() / fclose()
fopen() establishes a connection to a file or URL in the chosen mode; fclose() terminates that connection
and saves any pending changes.
Example:
<?
$fp = fopen("[Link]", "c+");
$val = (int)fread($fp, 1024) + 1;
rewind($fp);
fwrite($fp, $val);
fclose($fp);
?>
7. dirname()
Provides the directory path leading up to the last slash in a given pathname.
Example:
<?
echo dirname("/a/b/c.");
?>
8. fgetcsv()
Retrieves a single line from the file handle and interprets it as values separated by commas, outputting
them as a numbered array.
Example:
<?
$fh = fopen("[Link]", "r");
while (($row = fgetcsv($fh)) !== false) {
print_r($row);
}
fclose($fh);
?>
9. fwrite()
Safely writes the given string to the open file stream without altering binary data.
Example:
<?
$fp = fopen("[Link]", "w");
fwrite($fp, "Hello world");
fclose($fp);
?>
10. file()
Loads the whole file into an array, with each element representing a line including the newline characters.
Example:
<?
$lines = file("[Link]");
foreach ($lines as $line) {
echo htmlspecialchars($line) . "<br>";
}
?>
11. file_get_contents()
Retrieves all data from a file or remote URL and returns it as one continuous string.
Example:
<?
$html = file_get_contents("[Link]
echo substr($html, 0, 200);
?>
12. filesize()
Gives the byte count for the specified file or link.
Example:
<?
echo filesize("[Link]");
?>
13. is_dir()
Checks if the provided path points to an existing directory and returns true or false.
Example:
<?
var_dump(is_dir("/tmp"));
var_dump(is_dir("/etc/passwd"));
?>
14. link()
Establishes a hard link that references the same underlying data as the target file.
Example:
<?
link("[Link]", "[Link]");
?>
15. rename()
Moves or renames a file or directory from its current path to a new one, ensuring the operation is atomic on
the same filesystem.
Example
<?
rename("[Link]", "[Link]");
rename("/tmp/[Link]", "/gallery/[Link]");
?>
16. filetype()
Identifies the category of the filesystem item with a simple string like "file", "dir", "link", "fifo", "char", "block",
or "unknown".
Example:
<?
echo filetype(__FILE__);
echo filetype(__DIR__);
?>