You're probably looking at dirname()
:
<?php echo dirname($image->url()); ?>
For example:
echo dirname('http://example.org/path/to/picture.jpg');
// http://example.org/path/to
Question
SHORT VERSION OF MY QUESTION: Is there a way using PHP to echo the URL of the directory containing an image?
LONG VERSION: Hello all, I am working on a simple CMS (using Kirby in case it's relevant) and I've got almost everything working except for one thing. Kirby is a file-based CMS and each folder is its own page. I have it set so that all the images in a folder are automatically placed on the page. This is all working fine, but the thing is, I want to have a subfolder inside each project folder containing larger versions of the images. I don't want to use an automatic thumbnail plugin, or lightbox or anything like that. I'd like to manually save both versions of the image. I envisioned something like this: by clicking on the "thumbnail" image
website.com/projects/01-test/cat.jpg
could open a larger image
website.com/projects/01-test/large/cat-large.jpg)
I was hoping that I could somehow use PHP to display the full URL up to the directory of the image, but not the name of the image itself. The closest I have come is using:
<?php echo $image->url(); ?>
but that gives http://website.com/projects/01-test/cat.jpg while I just need http://website.com/projects/01-test.
If I could somehow automate this, then I figured I could do something like:
<a href="<?php echo $image->MYSTERY(); ?>/large/<?php echo $image->name(); ?>-large.jpg">
Okay, I hope this makes sense. I am learning PHP as I go along so I apologize if this has been covered before—I have tried searching everywhere but it might just be that I don't quite know what to search for. Any help you can offer would be greatly appreciated!
Solution
You're probably looking at dirname()
:
<?php echo dirname($image->url()); ?>
For example:
echo dirname('http://example.org/path/to/picture.jpg');
// http://example.org/path/to
OTHER TIPS
Explode your URL to be an array and remove last element. using explode() - array_pop() - implode()
like this:
$arrayofurl = explode("/", $url);
array_pop($arrayofurl);
$final = implode("/",$arrayofurl);
From your question, I assum that $image->url();
returns a full image URL and $image->name();
returns the name (without ext) of the image, right?
So I think this should do the trick, shouldn't it?
$urlPath = str_replace($image->name() . '.jpg', '', $image->url());
printf('<a href="%slarge/%s-large.jpg"><img src="%s"/></a>', $urlPath, $image->name(), $image->url());
Update: Forget about this answer, just use dirname()
.
Just for the fun of it, below a JS alternative that will iterate over all image links and set the XL image path:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<a href="#"><img src="test.jpg"/></a><hr>
<a href="#"><img src="test2.jpg"/></a><hr>
<a href="#"><img src="test3.jpg"/></a>
<script>
for(var a = document.getElementsByTagName("a"), b = 0;b < a.length;b++) {
var c = a[b], d = new String(c.children[0].src), e = d.replace(/\\/g, "/").replace(/\/[^\/]*\/?$/, ""), f = d.replace(e, "");
c.href = e + "/large" + f.replace(".jpg", "-large.jpg")
}
;
</script>
</body>
</html>