note 108190 added to function.filetype
| From: | holdoffhunger at gmail dot com | Date: | Thu, 05 Apr 2012 19:23:51 +0000 |
| Subject: | note 108190 added to function.filetype | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186988@lists.php.net to get a copy of this message | ||
Something you may eventually want to do is to know the type of file simply by the suffix of the
filename. Functions like finfo_file and mime_ content_ type come close, but they don't
actually list the filename suffix (like "txt" for "readme.txt"), and this
filetype function seems to list "directory" or "file" only.
Knowing the filename suffix can be very useful, especially when managing a filesystem that has
copies. It's better to name the copy of a file as "readme(copy-1).txt" rather than
"readme.txt(copy-1)", since the latter option probably isn't going to cooperate so
well with your text editor/reader. Here's a little code below that returns the suffix of a
filename in a string. It returns both the prefix and the suffix of the filename :
<?php
// Example Filename: "Homepage.php"
// ------------------------------------
$file_name = "Homepage.php";
// Filename Data
// ------------------------------------
$length_of_filename = strlen($file_name);
$last_char = substr($file_name, $length_of_filename - 1, 1);
// Parse Filename Backwards
// ------------------------------------
for($i_parse_name = 0; $i_parse_name < $length_of_filename; $i_parse_name++)
{
// Gather Data and Detect
// ------------------------------------
$last_char = substr($file_name, $length_of_filename - $i_parse_name + 2, 1);
if($last_char == ".")
{
$filename_suffix = substr($file_name, $length_of_filename - $i_parse_name + 2, $i_parse_name);
$filename_prefix = substr($file_name, 0, $length_of_filename - strlen($filename_suffix));
$i_parse_name = $length_of_filename;
}
}
// Print Results
// ------------------------------------
print("Filetype Results -- $filename_prefix ||| $filename_suffix");
// Example Results:
// ------------------------------------
// Filetype Results -- Homepage ||| .php
?>
Other examples:
"Best.Page.in.the.Universe.xml"
Filetype Results -- Best.Page.in.the.Universe ||| .xml
"Best.Page.in.the.Universe.xml5789"
Filetype Results -- Best.Page.in.the.Universe ||| .xml5789
"Home.awesome.page.php"
Filetype Results -- Home.awesome.page ||| .php
----
Server IP: 69.147.83.197
Probable Submitter: 68.9.137.178
----
Manual Page -- http://www.php.net/manual/en/function.filetype.php
Edit -- https://master.php.net/note/edit/108190
Del: integrated -- https://master.php.net/note/delete/108190/integrated
Del: useless -- https://master.php.net/note/delete/108190/useless
Del: bad code -- https://master.php.net/note/delete/108190/bad+code
Del: spam -- https://master.php.net/note/delete/108190/spam
Del: non-english -- https://master.php.net/note/delete/108190/non-english
Del: in docs -- https://master.php.net/note/delete/108190/in+docs
Del: other reasons-- https://master.php.net/note/delete/108190
Reject -- https://master.php.net/note/reject/108190
Search -- https://master.php.net/manage/user-notes.php