deemi-D-nadeem
deemi-D-nadeem

Reputation: 2514

How to split image name from path in php wordpress

I am trying to split the image name(with ext) from a path and save it and also save the path without image name in other field in DB. But i dont get any solution that how can i split it.

i use this code it gives me only the image name not the path here is my code

$url = $dataa['file_url'];          
$path = substr( $url, strrpos( $url, '/' ) );

It gives me only image name not the path.

My images name are dynamically and also paths are dynamically like this

http://www.werb_url.com/wp-content/uploads/2014/06/1.jpg
http://www.werb_url.com/wp-content/uploads/2014/06/first.png
http://www.werb_url.com/wp-content/uploads/2014/07/2.jpg
http://www.werb_url.com/wp-content/uploads/2014/07/second.gif

I want image path "behalf of image name" and image name like this

$path = http://www.werb_url.com/wp-content/uploads/2014/06/
$image = 1.jpg

How can i get the image path from image name. Any idea how can i do this

Upvotes: 0

Views: 1117

Answers (5)

Siddique Aboobacker
Siddique Aboobacker

Reputation: 39

html>
 <head>
   <title>Image name and path</title>
 </head>
 <body>
  <?php
   $url = 'http://www.werb_url.com/wp-content/uploads/2014/06/1.jpg';
   $image_name = end(explode('/',$url));
   $image_path = str_replace($image_name, "", $url);
   print_r("Image Name : ".$image_name."</br>Image Path   : ".$image_path);
  ?>
</body>

Upvotes: 1

NaveenThally
NaveenThally

Reputation: 1014

check out this

$url="http://www.werb_url.com/wp-content/uploads/2014/06/1.jpg";
$imagename=basename($url);
$myurl=str_replace($imagename,'',$url);
echo $myurl."<br />";
echo $imagename;

Upvotes: 1

Nithyanandhan M
Nithyanandhan M

Reputation: 1554

Try this...

$imagename=basename($Image_path);

Upvotes: 1

Anjith K P
Anjith K P

Reputation: 2158

Then use pathinfo

$pathinfo = pathinfo('http://www.werb_url.com/wp-content/uploads/2014/06/1.jpg');
$pathname = $pathinfo['dirname'];
$filename = $pathinfo['basename'];

Upvotes: 1

Shah Rukh
Shah Rukh

Reputation: 3156

you can use php pathinfo function

Upvotes: 1

Related Questions