I have the following code to grab the filename when browsed for and uploaded and take off the . and the extension

$FileName=current(explode('.', $_FILES['File']['name']));

only it causes problems if there is a dot in the file name, like
04.05.2007_filename.mp3
Is there another way to do this?

    preg_replace( '/.[a-z0-9]+$/i' , '' , 'dotted.file.Name' );

    might do the job.

      There is [man]pathinfo/man. Your current method would work if you discard the last element and join back the rest.

        for example

        $FileNameTokens = explode('.', $_FILES['File']['name']);
        $fileName = implode(".", array_slice($FileNameTokens, 0, count($FileNameTokens) - 2));
        

        or

        $FileNameTokens = explode('.', $_FILES['File']['name']);
        array_pop($FileNameTokens);
        $fileName = implode(".", $FileNameTokens);
        

        or with regular expressions (which would be the cleanest option in my opinion)

          $path_parts = pathinfo($_FILES['File']['name']);
          $FileName = $path_parts['filename'];

            dagon wrote:

            $path_parts['filename'];

            hadnt actually stumbled upon that baby yet, since i installed v5.2.0... thanks.

              hadnt actually stumbled upon that baby yet, since i installed v5.2.0... thanks.

              Actually, even prior to PHP 5.2 there is:

              $path_parts = pathinfo($_FILES['File']['name']);
              echo basename($path_parts['basename'], '.' . $path_parts['extension']);

              Though now I would use:

              echo pathinfo($_FILES['File']['name'], PATHINFO_FILENAME);

              on account that the extension is not always present.

                Sweet, learn something new every day.

                So, I should do this

                $path_parts = pathinfo($_FILES['File']['name'], PATHINFO_FILENAME);
                $FileName = $path_parts['filename'];

                instead of this?

                $path_parts = pathinfo($_FILES['File']['name']);
                $FileName = $path_parts['filename'];

                  If you prefer a regex solution:

                  $name = preg_replace('/\.[^.]*$/', '', $dottedName);
                  

                    So, I should do this

                    Not quite. If you only specify one path element, then it is returned as a string, not an array. So, if you are using PHP 5.2 (or later), you can specify PATHINFO_FILENAME and get it immediately.

                    $FileName = pathinfo($_FILES['File']['name'], PATHINFO_FILENAME);
                      Write a Reply...