[SOLVED] Removing extension from uploaded file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

[SOLVED] Removing extension from uploaded file

Post 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!
Last edited by p3rk5 on Wed Feb 18, 2009 10:20 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Removing extension from uploaded file

Post by requinix »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Removing extension from uploaded file

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Removing extension from uploaded file

Post 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"...
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

Re: Removing extension from uploaded file

Post 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?
vmmene
Forum Newbie
Posts: 8
Joined: Mon Feb 09, 2009 3:26 am

Re: Removing extension from uploaded file

Post by vmmene »

just break it by period.use explode function of php to do this.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Removing extension from uploaded file

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

Re: Removing extension from uploaded file

Post 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
Post Reply