Re: Using split to establish extension of file

From: Date: Wed, 07 Jul 2004 10:42:50 +0000
Subject: Re: Using split to establish extension of file
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-189858@lists.php.net to get a copy of this message
Many thanks - just what I was looking for! Cheers Russell -- ----------------------------------- Russell Curtis M +44 (0) 7796268780 www.russellcurtis.com www.buildingdetails.co.uk "Ewout" <spamme001@xs4all.nl> wrote in message news:001b01c4640d$a5599340$0200410a@soyuz... > > ----- Original Message ----- > From: "Russell Curtis" <mail@russellcurtis.com> > To: <php-general@lists.php.net> > Sent: Wednesday, July 07, 2004 12:24 PM > Subject: [PHP] Using split to establish extension of file > > > > I'm trying to use the split function to identify the extension of an > > uploaded file: > > > > $file_name = $_FILES['userfile']['name']; > > $pieces = explode(".", $file_name); > > $file_extension = $pieces[1]; // piece2 > > > > Which will work, providing that the uploaded file only has one full-stop > in > > the name (so 'thisfile.pdf' will return 'pdf' as the $file_extension, for > > example.) > > > > However, there is a chance that someone will try and upload a file which > has > > more than one full-stop in it (eg. 'this.file.pdf') which will of course > > return 'file' as the variable $file_extension. > > > > My question is, is there any way to determine what the *last* result of > the > > array is rather than working from the first? > > > > This may be a basic question, but I'm a newbie; apologies in advance if > this > > is a simple one. I'm using PHP4, BTW, if that makes a difference. > > You can get the last element of an array using the 'array_pop' function: > > $last = array_pop($pieces); > > see http://www.php.net/manual/en/function.array-pop.php for details > > > > regards, > Ewout

« previous php.general (#189858) next »