I need some code on how to take a file name and extention and seperate them from eachother and put them into two separate variables.

The PHP book I have, from Sams, doesn't have anything about that subject at all. All they have is manipulating the file contents, and finding out information about the file.

Any help is appreciated

thank you

Longbow

    this in not a perfect method because it doesnot allow for multiple .'s in filenames. just an idea though, hope this is helpful.

    <?php
    $filename = "blah blah blah.txt";
    
    $filenameandextension = explode('.', $filename);
    
    $filename = $filenameandextension[0];
    
    $extension = $filenameandextension[1];
    
    echo "The filename is " . $filename . ". The extension is " . $extension . ".";
    
    ?>

    http://uk2.php.net/manual/en/function.explode.php

      list($filename, $ext) = split(".", $filename);
      

      if that does not work use this:

      list($filename, $ext) = split("\.", $filename);
      

      I do not remember if you have to escape the period or not.

      --FrosT

        Use [man]explode[/man] instead of [man]split[/man]

        Split uses regular expressions, which you do not need.

          <?php
          function checkLastDotInFileName($filename){
           for($i = strlen($filename); $i > 0; $i--){
            $test = substr($filename, $i, strlen($filename));
            if(eregi("\.", $test)){
             return $i;
            }
           }
           return 0;
          }
          $file = "erwoeg.gfer.dat";
          $num = checkLastDotInFileName($file);
          echo "Filename: ".substr($file, 0, $num)."<br>";
          echo "Extension: ".substr($file, $num, strlen($file));
          ?>
          

          This will check the last dot, incase there is multiple dots. Returns the number where it is the last dot, which you can then use substr to find it again.
          Returns false if there is no dot.

            Thanks! all that help really helped out alot. i appriciate everything
            ill be back for some more help if i need it

            Longbow

              I suggest that you use kitchin's suggestion of [man]pathinfo/man with [man]basename/man since it both deals with the case of multiple periods in the filename, and with the case where your initial filename has a directory path prepended.

              Still, if you want to do what execute's code does, you should use:

              if (($period_pos = strrpos($init_filename, '.')) !== false) {
              	$filename = substr($init_filename, 0, $period_pos);
              	$extension = substr($init_filename, $period_pos + 1);
              } else {
              	$filename = $init_filename;
              	$extension = '';
              }

              or

              $filename = explode('.', $init_filename);
              $extension = (count($filename) > 1) ? array_pop($filename) : '';
              $filename = implode('.', $filename);

              where $init_filename is the original filename that you want to parse.

                Write a Reply...