doa24uk Posted May 16, 2009 Share Posted May 16, 2009 Hi guys, I am currently using this code to split the extension from a file, this is working fine. However my file has 2 extensions & I want to return them both eg. filename.ext1.ext2 The code currently spits out .ext2 And I need it to spit out .ext1.ext2 //This function separates the extension from the rest of the file name and returns it function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; If it matters, both extensions will always be the same length (and same length as each other... eg. -> .mp3.txt Thanks in advance for the help! Quote Link to comment https://forums.phpfreaks.com/topic/158377-solved-get-extension-code-file-has-two-extensions/ Share on other sites More sharing options...
jackpf Posted May 16, 2009 Share Posted May 16, 2009 //This function separates the extension from the rest of the file name and returns it function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-2; $exts = $exts[$n]; $n2 = count($exts)-1; $exts2 = $exts[$n2]; return $exts.$exts2; Something liek that? Quote Link to comment https://forums.phpfreaks.com/topic/158377-solved-get-extension-code-file-has-two-extensions/#findComment-835226 Share on other sites More sharing options...
doa24uk Posted May 16, 2009 Author Share Posted May 16, 2009 This is nearly working but unfortunately it's breaking the rest of the script because I'm not sure what variables to change further on in the script.... Here's the full script //This function separates the extension from the rest of the file name and returns it function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-2; $exts = $exts[$n]; $n2 = count($exts)-1; $exts2 = $exts[$n2]; return $exts.$exts2; } //This applies the function to our file $ext = findexts ($_FILES['uploaded']['name']) ; //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. $ran = rand () ; //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This assigns the subdirectory you want to save into... make sure it exists! $target = "directory/"; //This combines the directory, the random file name, and the extension $target = $target . $ran2.$ext; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "Congratulations - File upload Successful!<br><br><h2>File Links</h2>http://mysite.com/directory/directory2/".$ran2.$ext."some text after the link<br>"; } else { echo "Oops! There was a problem uploading your file, please try again."; } Quote Link to comment https://forums.phpfreaks.com/topic/158377-solved-get-extension-code-file-has-two-extensions/#findComment-835230 Share on other sites More sharing options...
thebadbad Posted May 16, 2009 Share Posted May 16, 2009 How is it breaking your script? The function should only affect the filename of the moved file. jackpf's function doesn't work as expected because the $exts array is reset. It can also be written much simpler: function findexts($filename) { $parts = explode('.', strtolower($filename)); list($b, $a) = array_reverse($parts); return "$a.$b"; } Should fit with the rest of your script as far as I can see. Quote Link to comment https://forums.phpfreaks.com/topic/158377-solved-get-extension-code-file-has-two-extensions/#findComment-835319 Share on other sites More sharing options...
doa24uk Posted May 16, 2009 Author Share Posted May 16, 2009 Brilliant, that's working now. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/158377-solved-get-extension-code-file-has-two-extensions/#findComment-835344 Share on other sites More sharing options...
Daniel0 Posted May 16, 2009 Share Posted May 16, 2009 Or: $filename = 'hello.world.foo.bar.test'; preg_match('#((?:\.[^.]+){1,2})$#', $filename, $matches); $ext = $matches[1]; // .bar.test Quote Link to comment https://forums.phpfreaks.com/topic/158377-solved-get-extension-code-file-has-two-extensions/#findComment-835356 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.