Using the below code snippet you can get any file extension or type and use in further development on the website. We can use explode PHP function to break the string or name of the file in the array and can get the last part of the array.
That last part will be file extension. With the help of explode function, you can break the string in an array and get the last array value using the PHP end function.
In the following code, you can use pathinfo PHP function to get file extension. In the below function, strlen finds the total length of the string and strrpos get the count of left-hand string from dot. Before you try these out, create a new PHP page and save it as fileDir.
To get the exact path of file, then, you can use realpath. In between the round brackets of the function, type the name of the file. To get the names of the folders, you can use the dirname function. This will strip off the name of the file and return the rest of the text between the round brackets of the function. Caution pathinfo is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale function.
Parameters path The path to be parsed. Return Values If the flags parameter is not passed, an associative array containing the following elements is returned: dirname , basename , extension if any , and filename. Note: pathinfo is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale function. If a file has more than one 'file extension' seperated by periods , the last one will be returned.
Note that this function seems to just perform string operations, and will work even on a non-existent path, e. Checked with version 5. Here is a simple function that gets the extension of a file. Note that in PHP 4 if you're stuck using it , pathinfo only provides dirname, basename, and extension, but not filename. This function will not split a file's stem and extension for you. So you'll need to define it's value manually if the option field is omitted, to provide the default functionality.
0コメント