note 54726 added to function.basename
| From: | pvollma at pcvsoftware dot net | Date: | Wed, 13 Jul 2005 18:43:52 +0000 |
| Subject: | note 54726 added to function.basename | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91917@lists.php.net to get a copy of this message | ||
There is a real problem when using this function on *nix servers, since it does not handle Windows
paths (using the \ as a separator). Why would this be an issue on *nix servers? What if you need to
handle file uploads from MS IE? In fact, the manual section "Handling file uploads" uses
basename() in an example, but this will NOT extract the file name from a Windows path such as C:\My
Documents\My Name\filename.ext. After much frustrated coding, here is how I handled it (might not be
the best, but it works):
<?php
$filen = stripslashes($_FILES['userfile']['name']);
$newfile = basename($filen);
if (strpos($newfile,'\\') !== false) {
$tmp = preg_split("[\\\]",$newfile);
$newfile = $tmp[count($tmp) - 1];
}
?>
$newfile will now contain only the file name and extension, even if the POSTed file name included a
full Windows path.
----
Manual Page -- http://www.php.net/manual/en/function.basename.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54726
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54726&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54726&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54726&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54726&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54726&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54726&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54726&report=yes
Search -- http://master.php.net/manage/user-notes.php