How can the pathinfo() function be used to extract file extensions in PHP?
To extract file extensions in PHP, the pathinfo() function can be used. This function takes a file path as input and returns an associative array containing information about the file, including the file extension. By accessing the 'extension' key of the array, we can easily retrieve the file extension.
$file_path = '/path/to/file/example.txt';
$file_info = pathinfo($file_path);
$file_extension = $file_info['extension'];
echo $file_extension; // Outputs 'txt'
Keywords
Related Questions
- What are the implications of setting directory permissions to 777 in a PHP script for uploading files?
- What alternative approach, mentioned by another user in the thread, can be used to pass values from a form to a PHP script for database storage instead of using JavaScript?
- What are the potential pitfalls of using preg_split to extract information from HTML strings in PHP?