Thanh Nguyen
Thanh Nguyen

Reputation: 5342

PHP get file path of an image?

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/

Upvotes: 7

Views: 25626

Answers (2)

Madarco
Madarco

Reputation: 2114

To get the parent folder of a file:

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

See: the doc

Upvotes: 6

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

Use dirname()

<?php
echo dirname('localhost/example/folder/filename.jpg');
?>

Upvotes: 8

Related Questions