Jump to content

[SOLVED] Get extension code -> file has two extensions ???


doa24uk

Recommended Posts

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!

Link to comment
Share on other sites

//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?

Link to comment
Share on other sites

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.";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.