0

I am basically after a method of extracting a file name from a path. The file type may change and the file name may have white space in it. Also the path may be in Unix form or Windows form.

/Users/Bob/Documents/some file.docx

Or

C:\Users\Bob\My Documents\some file.docx

The complete source path is already stored in a variable called $fileSource and I have the file name without the extension in a variable called $fileName. I have tried to use str_replaceto replace the common part of the file source with zero space but the path may change so this is not viable.

I thought if regex could be used to work backward through the string the last (reading from left to right) \ or / but I don't really know much regex yet. I have read about a look-backward feature which should start (?<=string) but I can't get this to work.

Lastly The reason I need the file name with the extension is because I am using PHP to copy it from one location to another and if I don't have the file extension the files turns up without the extension and I have to add it manually.

If what I am asking is not possible I guess I could just use regex to get the file extension and then concatenate that to the $fileName but I am hoping for something more graceful.

Cheers.

5
  • 2
    What's wrong with the built-in pathinfo() function, it is there for exectly this purpose Commented Jul 9, 2013 at 8:45
  • +1 for pathinfo. Commented Jul 9, 2013 at 8:46
  • Check the PHP Manual at basename Commented Jul 9, 2013 at 8:46
  • True, $ext = pathinfo($filename, PATHINFO_EXTENSION); would be the slickest IMHO. Commented Jul 9, 2013 at 9:01
  • It's not clear what you want to achieve; does basename($fileSource) work? If not, why not? Commented Jul 9, 2013 at 9:04

3 Answers 3

3

It's incredible simple to use pathinfo:

$fileName = pathinfo($fileSource, PATHINFO_BASENAME);

..or simply basename:

$fileName = basename($fileSource);

Do note: Windows paths will not work correctly on linux servers and linux paths will not work correctly on windows servers (due to DIRECTORY_SEPERATOR being different).

DEMO on a linux server

Sign up to request clarification or add additional context in comments.

6 Comments

@Jack Correct - added info to the answer.
@Jack Except the extension is added. Surely you could do basename($fileSource) . '.' . pathinfo($fileSource, PATHINFO_EXTENSION) but you'd be calling 2 functions instead of 1, and readability wouldn't be any easier (in my opinion), so hence why I used but one call to pathinfo :)
Though if you're using < 5.2, you really should upgrade.... even if you're using 5.2.x, you really should upgrade
^ This. The reason I didn't add it in my answer is that some people simply can't upgrade due to different reasons. Anything from no-access-servers and crappy programmed software could result in you not being able to go past 5.1.
@Jack PATHINFO_FILENAME doesn't strip the extension. basename would be "document", extension would be doc and filename would be document.doc. Check the manual. Edit: Oh wow - maybe I should check the manual. Fixing the answer.
|
1

PHP's pathinfo is specifically for this:

Examples:

$file = pathinfo($yourFile, PATHINFO_FILENAME);

$file_ext = strtolower(pathinfo($yourFile, PATHINFO_EXTENSION)); //gets the file extension

Comments

0

Try this (alternative to pathinfo):

<?php
$filePath = '/Users/Bob/Documents/some file.docx'; // using *nix here
$file     = end(explode(DIRECTORY_SEPARATOR, $filePath));

echo $file;

In case you worry about paths without a filename (so a directory path), then use this:

<?php
$filePath = '/Users/Bob/Documents/some file.docx'; // using *nix here
$DS       = DIRECTORY_SEPARATOR; // this way you can handle WinOS and *NIX paths
$file     = end(explode($DS, trim($filePath, $DS));

// Check if the path contains a dot, so it's a file name 
// with extension (e.g. filename.php)
$file     = (strpos($file, '.') !== false) ? $file : false;

echo $file;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.