🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

CP476 Internet Computing Week 4 – 1 PHP – file handling Shaun Gao, Ph.D., P.Eng. Objectives Introduction File handling Open and read Create and write File upload PHP filters Two types of filters Summary Introduction File access is a common topic for any programming languages. Input and output as kno...

CP476 Internet Computing Week 4 – 1 PHP – file handling Shaun Gao, Ph.D., P.Eng. Objectives Introduction File handling Open and read Create and write File upload PHP filters Two types of filters Summary Introduction File access is a common topic for any programming languages. Input and output as known as I/O General procedure Open Read Write close PHP file handling PHP readfile() Function - Reads a file and writes it to the output buffer. Syntax readfile(string $filename, bool $use_include_path = false, resource $context = ?): int | false if you want to search for the file in the include_path, $use_include_path = true. A context is a set of parameters which modify or enhance the behavior of a stream. Return values: Returns the number of bytes read from the file on success, or false on failure Example: Demo1: PHP file handling – cont. PHP File Open/Read/Close – C language like functions. PHP Open File - fopen() Syntax fopen( string $filename, string $mode, bool $use_include_path = false, resource $context = ? ): resource | false Return values: success: a file resource (ID), or false on failure PHP file handling – cont. PHP File Open/Read/Close – C language like functions. PHP Open File - fopen() Example Demo2: File open mode PHP file handling – cont. PHP File Open/Read/Close – C language like functions. PHP Read File - fread() - The fread() function reads from an open file. Syntax fread(resource $stream, int $length): string | false fread() reads up to length bytes from the file pointer referenced by stream that is created by fopen() function. Return values: success the read string or false on failure. PHP file handling – cont. PHP File Open/Read/Close – C language like functions. PHP Read File - fread() - The fread() function reads from an open file. Example: Demo3: 1) success; 2) fail to open PHP Read Single Line – fgets() The fgets() function is used to read a single line from a opened file. Syntax fgets(resource $handle, int $length = ?): string | false Return values: Returns a string of up to length - 1 bytes read from the opened file. If an error occurs, false is returned. Demo4: fread() vs fgets() in PHP Syntax fread(resource $handle, int $length): string | false fgets(resource $handle, int $length = ?): string | false In general fread — Binary-safe file read. fread() reads up to length bytes. fgets — Gets a line from file pointer. The ‘fgets’ function reads a line and stops when it encounters a newline if $length is not given. If the user wishes to read a line from a text file, then ‘fgets’ function is suggested. If the user wishes to read raw data from a file, then ‘fread’ function is suggested. PHP file handling – cont. PHP File Open/Read/Close – C language like functions. PHP Close File - fclose(), it is used to close an open file. Syntax fclose(resource $stream): true | false It's a good programming practice to close all files after you have finished with them. Return values: Returns true on success or false on failure. PHP Check End-Of-File - feof() The feof() function checks if the "end-of-file" (EOF) has been reached. Syntax feof(resource $stream): bool Return values: true if EOF reached or an error occurs (including socket timeout); otherwise returns false. The feof() function is useful for looping through whole file. $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); while(!feof($myfile)) { echo fgets($myfile); } fclose($myfile); Demo5: PHP Write to File - fwrite() The fwrite() function is used to write to a file. Syntax fwrite(resource $handle, string $string, int $length = ?): int | false fwrite() writes the contents of string to an opened file pointed to by $handle. Return values: returns the number of bytes written, or false on error.

Use Quizgecko on...
Browser
Browser