I was using the strrchr PHP function with substr and strrpos to find the file name in a string with the full path like:
/images/onepiece.jpg returns
onepiece.jpg
but now I need to a function to find not the last “/” but the one next to the last:
/images/anime/onepiece.jpg returning anime/onepiece.jpg or /anime/onepiece.jpg
And as strrchr – 1 doesn’t work, hehehe :), how can I achieve that?
[SOLVED]
Using PHP pathinfo() as @middaparka and @Shakti Singh said, I’ve change the way I get the image string from the MySQL database. Now it can have subfolders, as was my initial intention.
<?php
/*
* pathinfo() parameters:
* PATHINFO_DIRNAME = 1
* PATHINFO_BASENAME = 2
* PATHINFO_EXTENSION = 4
* PATHINFO_FILENAME = 8
* */
$slash = '/';
$mainpath = 'store/image/';
$thumbspath = 'cache/';
$path = $imgsrow->photo; //gets the string containing the partial path and the name of the file from the database
$dirname = pathinfo($path, 1); //gets the partial directory string
$basename = pathinfo($path, 2); //gets the name of the file with the extension
$extension = pathinfo($path, 4); //gets the extension of the file
$filename = pathinfo($path, 8); //gets the name of the file
$dims = '-100x100.'; //string of size of the file to append to the file name
$image = $mainpath . $path; //mainpath + path is the full string for the original/full size file
$thumbs = $mainpath . $thumbspath . $dirname . $slash . $filename . $dims . $extension; //string to point to the thumb image generated from the full size file
?>
<img src="<?= $thumbs; ?>" width="100" height="100" alt="<?= $row->description; ?>" />
<br />
<img src="<?= $image; ?>" width="500" height="500" alt="<?= $row->description; ?>" />
To be honest, it would be a lot easier to use the pathinfo or dirname functions to decompose directory paths.
For example:
You may have to use a mix of these to obtain what you’re after but they will at least be OS “safe” (i.e.: will handle both Linux/Linux and Windows path styles).
In terms of the specific problem you have, you should be able to use the following cross-platform solution to get what you need: