0

hello. i have some problem while uloading song or image in php...if i have a song name like "05.Judaiyaan [MusikMaza.Com].mp3" then it have double dots...and i am unable to retrieve mp3 extension...can anyone help me out???

3
Contributors
4
Replies
25
Views
3 Years
Discussion Span
Last Post by diafol
0

Show your code.You may need to sanitize your filename.

0

Hi,

If you are running PHP version 5.3.6 or higher, this should work

<?php

function get_file_ext($filename){

        $info = new SplFileInfo($filename);
        return($info->getExtension());
}

to test the function above try..

echo get_file_ext('05.Judaiyaan [MusikMaza.Com].mp3');

that should output mp3

side notes: If you will be renaming the uploaded file, you need to get the filename without extension. To get the filename without extension, we can simply do it like this.

echo '<br/>'. basename('05.Judaiyaan [MusikMaza.Com].mp3', '.'.get_file_ext('05.Judaiyaan [MusikMaza.Com].mp3'));

the above should give us

05.Judaiyaan [MusikMaza.Com]

make sure to sanitize the filename as suggested above by Diafol.

Edited by veedeoo: info added.

0

my code is :

if(isset($_FILES["song_url"]['name']))
            { 
                    $fname =  explode('.',$_FILES["song_url"]['name']);
                    $ext = $fname[1];
                    $filen1 = uniqid().".".$ext;//$_FILES["ic_pic"]['name']; //file name
                    $path1  = 'songs/'.$filen1; //generate the destination path
                    move_uploaded_file($_FILES["song_url"]['tmp_name'],$path1);

             }
0

The explosion is not the way to get the extension. Either use pathinfo or veedeoo's SPL. Example...

$f = "05.Judaiyaan [MusikMaza.Com].mp3";
echo pathinfo($f,PATHINFO_EXTENSION); 

$info = new SplFileInfo($f);
echo $info->getExtension();

Anyway...

<?php

if(isset($_FILES['song']))
{
    if($_FILES['song']['error'])
    {
        echo "Bad upload: error " . $_FILES['song']['error'];
    }else{
        $f = $_FILES['song'];

        $ext = pathinfo($f['name'],PATHINFO_EXTENSION);
        $filename = uniqid().".".$ext;
        $dest = 'uploads/' . $filename;
        move_uploaded_file($_FILES["song"]['tmp_name'], $dest);
        $downloader = "<a href='$dest'>$dest</a>";    
    }
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form enctype="multipart/form-data" method="post"  >
    <input type="file" name="song" />
    <input type="submit" value="Upload" name="uploadbutton" /> 
</form>
<?php if(isset($downloader)) echo $downloader;?>
</body>
</html>

Gives the link of the newly uploaded and renamed mp3 file.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.