Page 1 of 1

[SOLVED] Removing extension from uploaded file

Posted: Tue Feb 17, 2009 4:25 pm
by p3rk5
I have a script that uploads files to my server. However I need to implement a way to remove the file extension from the file at the time of upload. I've used Google to try and find some ways of doing this but I can't seem to get those scripts to work. Here is my code for add.php:

Code: Select all

<? session_start();?>
<?php
set_time_limit(0);
$logged_in = $_SESSION['logged_in'];
$level = $_SESSION['level'];
$dbhost='***';
$dbusername='***';
$dbuserpass='***';
$dbname3='music';
$submit = $_POST['submit'];
if ($logged_in && $level <= 2) {
    if (isset($submit)) {
        $artist = $_POST['artist'];
        $title = $_POST['title'];
        $genre = $_POST['genre'];
        $length = $_POST['min'].':'.$_POST['sec'];
        $quality = $_POST['quality'];
        $url = "music/songs/" . $_FILES['userfile']['name'];
        if (copy($_FILES['userfile']['tmp_name'], "music/songs/" . $_FILES['userfile']['name'])) {
            mysql_connect($dbhost,$dbusername,$dbuserpass) or die("MySQL Error");
            @mysql_select_db($dbname3) or die("Cannot select database");
            $query = "INSERT INTO `tracks` VALUES ('','$artist','$title','$genre','$length','$quality','$url')";
            mysql_query($query);
            mysql_close;
            echo '<h3>Add Music</h3><br><center><b>The song '.$title.' by '.$artist.' was successfully added</b></center>';
        } else {
            echo '<h3>Add Music</h3><br><center><b>Error uploading '.$title.' by '.$artist.'</b></center>';
        }
    } else {
        echo '<form enctype="multipart/form-data" action="index.php?id=music&page=add" method="post">
        <h3>Add Music</h3>
        <table border="0" align="center">
            <tr>
                <td>
                    <b>Artist:</b>
                </td>
                <td>
                    <input type="text" name="artist" />
            </tr>
            <tr>
                <td>
                    <b>Title:</b>
                </td>
                <td>
                    <input type="text" name="title" />
            </tr>
            <tr>
                <td>
                    <b>Genre:</b>
                </td>
                <td>
                    <select name="genre"><option value="" selected><option value="Abstract">Abstract</option><option value="Ambient">Ambient</option><option value="Breakbeat">Breakbeat</option></option><option value="Drum and Bass">Drum and Bass</option><option value="Electronic">Electronic</option><option value="Experimental">Experimental</option><option name="Hardcore">Hardcore</option><option value="Hip-Hop">Hip-Hop</option><option value="House">House</option><option value="Techno">Techno</option><option value="Trance">Trance</option></select>
                </td>
            </tr>
            <tr>
                <td>
                    <b>Length:</b>
                </td>
                <td>
                    <input type="text" name="min" size="1" maxlength="2" /><span>:</span><input type="text" name="sec" size="1" maxlength="2" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Quality:</b>
                </td>
                <td>
                    <input type="text" name="quality" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="file" name="userfile" class="file" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" value="Submit" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <a href="index.php?">Cancel</a>
                </td>
            </tr>
        </table></form>';
    }
} else {
    echo '<h1>Forbidden</h1><p class="logintxt">You are not authorized to view this page</p>';
}
?>
What do you think the best way to implement the code to remove the extension would be? Any help is much appreciated!

Re: Removing extension from uploaded file

Posted: Tue Feb 17, 2009 4:42 pm
by requinix

Re: Removing extension from uploaded file

Posted: Tue Feb 17, 2009 5:41 pm
by pickle
Actually, you want to find the last position of the period, so files like: report.2009.02.17.pdf work. Use strrpos() instead.

Re: Removing extension from uploaded file

Posted: Tue Feb 17, 2009 5:48 pm
by requinix
pickle wrote:Actually, you want to find the last position of the period, so files like: report.2009.02.17.pdf work. Use strrpos() instead.
If you use the last period then someone could upload a file called "bad.php.pdf"...

Re: Removing extension from uploaded file

Posted: Tue Feb 17, 2009 6:41 pm
by p3rk5
I'm not worried about people uploading bad files because the script is protected by a login system with a hierarchy. Should I still find the first period or the last one? Does it matter?

Re: Removing extension from uploaded file

Posted: Wed Feb 18, 2009 3:30 am
by vmmene
just break it by period.use explode function of php to do this.

Re: Removing extension from uploaded file

Posted: Wed Feb 18, 2009 10:12 am
by pickle
Find the last one - that way you're guaranteed (unless they upload a file without an extension) to only be removing the extension, and not possibly useful parts of the filename.

Re: Removing extension from uploaded file

Posted: Wed Feb 18, 2009 10:20 am
by p3rk5
pickle wrote:Find the last one - that way you're guaranteed (unless they upload a file without an extension) to only be removing the extension, and not possibly useful parts of the filename.
Ok will do, thank you very much