7

Example:

The image url:

localhost/example/folder/filename.jpg

I want to get path of the image so it must be:

localhost/example/folder/

How to do that?

I speak English not well. Thanks!

I have found a solution instead of using dirname:

$image = 'localhost/example/folder/filename.jpg';
$directory = substr($image, 0, strrpos($image, '/', -2) + 1 );
echo $directory;
//return: localhost/example/folder/
2

2 Answers 2

8

Use dirname()

<?php
echo dirname('localhost/example/folder/filename.jpg');
?>
Sign up to request clarification or add additional context in comments.

Comments

6

To get the parent folder of a file:

dirname("localhost/example/folder/filename.jpg");

See: the doc

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.