Why does this if statement not work?

Posted on 2011-04-21
Last Modified: 2012-05-11
Here's my code:

$filecheck = basename($_FILES['mp3_file']['tmp_name']);
// this will return whatever is past the final . in the file name
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (strtolower($ext) == 'mp3') {
echo "mp3";

The file in question is an mp3, but the code doesn't recognize that and I need to fix it. How?
Question by:brucegust
    LVL 8

    Accepted Solution

    What does $filecheck and $ext look like? (echo it and post it here so we can work from there)
    LVL 4

    Assisted Solution

    what does echo $ext; show?
    LVL 13

    Assisted Solution

    by:F Igor
    $_FILES['mp3_file']['tmp_name'] does not store the real filename.
    Generally points to a temp filename like /tmp/php0456399303.tmp

    If you want to check use
    $filecheck =$_FILES['mp3_file']['name'];

    This field stores the filename from browser

    for more information on $_FILES array.

    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    Why don't you use PHP pathinfo():
    LVL 27

    Assisted Solution

    by:Lukasz Chmielewski
    What does the
    show ?

    Author Comment

    Hey guys!

    I figured it out. The tmp_name thing is what was messing things up. Once I inserted that into the equation, I got what I needed. However, I'm still not where I need to be and if you're interested, head out to this question: and you can help me figure out why although my path and file type are correct, the mp3 file isn't uploading.

    Rock on!
    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    I have posted a better way to get files extension, why did you accepted "NOT A SOLUTION" comment such as #35442341 and didn't accepted my solution while it's helpful for other people?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now