Solved

strip directory name and extension from file name

Posted on 2010-09-23
6
347 Views
Last Modified: 2012-06-27
Hi,

I use this script to get pictures from a folder and display them.

I would like to strip the directory-name and the extension from "$num" (IN THE TITLE TAG) so the title will have the picture name only.

thanks a lot!
<?php 
						$files = glob("pictures/*.*"); 
						$files = array_reverse($files); for ($i=0; $i<count($files); $i++) { $num = $files[$i]; 
						echo "<a href='".$num."' rel='shadowbox[Mobile]' title=\"". $num ." | Navigate with the &#8249;&#8212; and &#8212;&#8250; arrowbuttons.\"><img src='imgsize.php?h=75&img=".$num."' style='border: solid 1px black;'></a>&nbsp;&nbsp;";}
						 ?>

Open in new window

0
Comment
Question by:peps03
6 Comments
 
LVL 3

Expert Comment

by:guitar7man
Comment Utility
Are there further directories within the pictures folder?
0
 
LVL 7

Expert Comment

by:ziceva
Comment Utility
This should do the trick:

$path = "/home/httpd/html/index.php";
$file = basename($path);
$file=substr($file,0,strrpos($file,'.'));

echo $file;
0
 
LVL 3

Expert Comment

by:wuff
Comment Utility
if all files have the same extension, e.g. ".jpg" you can provide this to basename() and you get the filename without extension.

$path = "/mydir/images/myimage.jpg";
$filename = basename($path, '.jpg');
echo $filename;

gives this output:

myimage
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:peps03
Comment Utility
thanks ziceva, but i dont really get it.

how do i fit that in the code above? --> title=\"". $num ."

the num has to be stripped from dir-name and .jpg

@quitar7man: no

thanks!
0
 
LVL 7

Accepted Solution

by:
ziceva earned 500 total points
Comment Utility
like so:

$files = glob("pictures/*.*");
$files = array_reverse($files);
for ($i=0; $i<count($files); $i++)
{
 $num = $files[$i];
 $short_num = basename($num);
 $short_num = substr($short_num,0,strrpos($short_num,'.'));
 echo "<a href='".$num."' rel='shadowbox[Mobile]' title=\"". $short_num ." | Navigate with the &#8249;&#8212; and &#8212;&#8250; arrowbuttons.\"><img src='imgsize.php?h=75&img=".$num."' style='border: solid 1px black;'></a>&nbsp;&nbsp;";
}
0
 

Author Closing Comment

by:peps03
Comment Utility
Thanks a lot Ziceva!

It works great!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now