File System

42 entries

basename Function Returns the trailing name component of a path. chmod / chown Function Changes a file's permission bits and owner: chmod sets rwx bits with an octal mode (the leading 0 is mandatory), chown changes the owner (only the superuser may give a file away); PHP's own chmod clears that path's stat cache — clearstatcache is needed for changes made OUTSIDE the process. clearstatcache Function Clears PHP's per-path cached file status (the stat family): long-running processes need it before re-querying a path changed externally; modern PHP already auto-invalidates most in-script writes. dirname Function Returns the directory portion of a path with the trailing name component removed. disk_free_space Function Returns the bytes free on the filesystem or partition holding the directory as a float, false on failure; disk_total_space supplies the total. fgetcsv / fputcsv Function The CSV read/write pair: fputcsv writes with RFC 4180 quoting rules and fgetcsv parses back; escape must be passed explicitly since 8.4 (empty string recommended); the quoted-comma/newline/doubled-quote traps are shown with an in-memory round trip. fgets Function Line-by-line reading: PHP fgets keeps the trailing newline in the return value (length-1 cap) and returns false at EOF - whether each language keeps the newline is the core teaching point. file Function Reads an entire file into a line array (newlines kept by default, stripped with FILE_IGNORE_NEW_LINES) - the array counterpart of file_get_contents' single string: pick file() for line-by-line work and file_get_contents for whole-text handling. file_exists Function Checks whether a file or directory exists. file_get_contents Function Reads an entire file into a string. copy / rename / unlink Function The three file operations: copy (silently overwrites the destination), move/rename (rename also works on whole directories), and delete (unlink removes a symlink itself); all three return bool. file_put_contents Function Writes a string to a file and returns the number of bytes written. fileatime / filectime / fileowner / filegroup / fileinode Function File metadata family: fileatime/filectime/fileowner/filegroup/fileinode read access time, inode-change time, owner/group and inode; together with filemtime/fileperms/filesize they complete the full stat family. Results are cached per path; failures return false. filemtime Function Returns the file's last modification time (mtime) as Unix epoch seconds, or false on failure; cached per path, so clearstatcache before re-reading. fileperms Function Returns the file's full access mode (including file-type bits, e.g. octal 100644); ANDing with 0777 extracts the plain permission bits. False on failure. filesize Function Returns the file size in bytes (false on failure); results are cached per path, so clearstatcache before re-reading. finfo_open / finfo_file / mime_content_type Function MIME type detection: finfo_open builds the detector, finfo_file classifies by header magic bytes (filename-independent), mime_content_type is the one-step shorthand; backed by the libmagic database, unknown magic yields a generic default instead of an error. flock Function Applies an advisory lock to an open stream: LOCK_SH shared read / LOCK_EX exclusive write / LOCK_NB non-blocking / LOCK_UN release - the classic tool for multi-process write safety. fopen Function Opens a file and returns a stream handle (resource) that the whole f* family operates on; complements the fopen_modes concept entry - this entry focuses on the handle you get back. fopen (mode family: r/r+/w/w+/a/a+/x/x+) Function Opens a file per a mode string — the 8 modes decide four semantics (read/write, existence policy, truncate-on-open, forced-append); every language has its own open-mode vocabulary. fpassthru / readfile Function Two engines for dumping files: readfile writes the whole file to the output buffer in one step and returns the byte count; fpassthru streams from the stream's current position to EOF - the memory-difference teaching point for large downloads. fread / fwrite Function Block read/write: fread reads up to length bytes and fwrite returns the bytes actually written - both can come up short, so loop-until-full-or-EOF is the cross-language streaming idiom. fscanf Function Parses fields from a file stream by a format string: the file-stream sibling of sscanf (sscanf covered in A24); without by-reference variables it returns the parsed array, and false at EOF; the return-value semantics (assigned-field count) align with the C fscanf family. fseek / ftell Function Random access: SEEK_SET/CUR/END whences paired with ftell - the basics of jumping around large files and locating in logs; SEEK_END needs a NEGATIVE offset to land before the end of file. fstat / lstat Function Fetches the whole stat field set in one call (fstat from an open handle, stat/lstat from a path): the full size/mode/mtime/ino/nlink family; lstat does NOT follow a symlink (it reports the link itself: size = length of the stored target path) while stat follows — the closing entry for the earlier fileperms/filesize/filemtime single-field trio. fsync / fdatasync Function PHP 8.1 forced durability: pushes buffered writes to the storage medium - fsync covers data AND metadata, fdatasync data only (POSIX-only; on Windows fdatasync is aliased to fsync); true means the data hit the medium. ftruncate Function Truncates or extends an open stream to a given length in place: shrink — bytes past the offset are dropped; grow — the file becomes larger and the new gap reads back as zero bytes (a sparse file). Combined with fseek+fwrite this is the common in-place rewrite pattern. getimagesize Function Image size detection: reads the file header (e.g. the PNG IHDR chunk) for width/height/MIME without decoding the whole image and without requiring GD — hand-rolled header parsing is the shared soul of the ten-language demo. is_readable Function Whether a file/directory exists and is readable (real UID/GID check, stat-cached); the family also has is_writable/is_executable. link / symlink / readlink Function A hard link is a second name for the SAME inode (nlink grows; deleting one name keeps the data), a symlink is a pointer holding a path string; readlink reads the stored target WITHOUT following; the classic dangling-link contrast: is_link and readlink succeed while file_exists (following semantics) is false. move_uploaded_file Function Moves an HTTP-uploaded temp file to a new location: it embeds the "must be a genuine upload" check (defeats forged tmp_name path attacks) and overwrites an existing destination; in the CLI SAPI there is no upload mechanism, so both this and is_uploaded_file always return false (the example demonstrates exactly that). parse_ini_file Function Parses an INI config file into an associative array: process_sections nests by section, INI_SCANNER_TYPED preserves bool/int types, and failure returns false. pathinfo Function Returns information about a path: directory name, file name, extension, and more. realpath Function Returns the canonicalized absolute pathname, resolving symlinks and . / .. components; the path must actually exist. stream_copy_to_stream Function Copies from $from's current position (seeking first when offset > 0) to EOF, capped by length, returning the byte count — large files need zero manual buffering (an internal chunked loop), and it is the native counterpart of Go's io.Copy. stream_filter_append Function Stream filter chains: attaches named filters (string.toupper, convert.base64-encode, ...) to a stream's read or write chain so all subsequent reads/writes are transformed automatically (read chain = transform on the way in, write chain = transform on the way out) — a PHP-unique stream decoration mechanism; mode can force READ/WRITE/ALL. stream_get_contents Function The handle-level "read to end": reads from the stream's current position to EOF (the counterpart of file_get_contents, which reads a whole path at once); length caps the bytes and offset seeks first. stream_get_meta_data Function Stream metadata: returns the stream's "dossier" — timed_out/blocked/eof/unread_bytes/stream_type/wrapper_type/mode/seekable/uri (plus crypto on TLS streams); "a stream is a resource, and a resource has a dossier" — the first tool for diagnosing timeouts and wrapper behaviour. sys_get_temp_dir Function Returns the temporary-file directory path (resolving the TMPDIR/TEMP/TMP environment family). The demo prints derived facts only - the path itself is environment-dependent, so it verifies "non-empty and absolute" instead. tempnam Function Temp-file naming: atomically creates a unique zero-byte file in the given directory (O_EXCL guards the TOCTOU race, mode 0600 on Unix) and returns the full path; the prefix is only a hint (first 63 chars, first 3 on Windows), with silent fallback to the system temp dir when the target is unwritable. tmpfile Function Creates an anonymous w+b temp file handle that is auto-deleted on fclose or script end - made for burn-after-use buffering and swap scenarios. umask Function Reads or sets the process permission mask: a new file's default mode is 0666 & ~umask; passing a mask sets it and returns the old one, omitting it only queries (complementary to chmod on existing files).