Here’s a puzzler. I have a variable that contains computer file paths. I need to separate the filename from the path.
Example:
From this:
$filepath = C:\\User\Me\Myfiles\file.jpg
I need this:
$path = C:\\User\Me\Myfiles\
$file = file.jpg
I know how to get the substrings of $filepath using substr. The problem is using “\” as a “needle” to get the position of the filename.
This causes an error:
$pos = strrpos("\",$filepath);
This doesn’t cause an error, but doesn’t give me a value for $pos:
$pos = strrpos("\\",$filepath);
As always, any help with be rewared with my eternal gratitude.
This is trying to find the position of
$filepathin the string"\\". Unless the$filepathis a single backslash, no position will be returned.You want to swap the two arguments around.
Also,
basename($filepath)orpathinfo($filepath, PATHINFO_BASENAME)can be used to get the file name.