PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 9th, 2012, 02:51 PM
poopertropper poopertropper is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 poopertropper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 31 sec
Reputation Power: 0
PHP5 - Noob Question about basename

Hey guys,

So I've been really trying my hand at PhP lately (not another noob!) and I ran into a strange issue with basename that I'm wondering if you guys can help me with.

To start out, I've written up the code below to go into a directory and pic out the images there to load into an array. This works great and I've been able to update a clients gallery rather quickly using it.

PHP Code:
// grab images for stuff

$dir2 '../fest/'.$year.'/'.$id.'/examples/';
if(!
is_dir($dir2)){
    
$dir2 '../fest/'.$year.'/path/ex/'

}
$allImgs2 = array();
$dh2 opendir($dir2);

while ((
$file readdir($dh2)) !== false)
{
    if (
$file!='Thumbs.db' && $file!='.' && $file!='..' && $file!='.DS_Store' )
    {
        
$allImgs2[] = $file
    }
}
closedir($dh2);
?> 


However I wanted to use the photos file name as a way to title each image in an H1 and I ran into problems. I thought basename would remove the .jpg part and then using CSS I would just set everything to CAPS for styling. So I wrote it like this:


PHP Code:
<ul id="slider">                                      
<?
php
     
foreach($allImgs2 as $img2){
     echo 
'<li><a class="fancybox" href="'.$dir2.$img2.'" title="'.$img2["basename"].'"><img src="'.$dir2.$img2.'"><h1>'.$img2["basename"].'</h1></li></a>';
}
?>
</ul> 


I figured using .$img2["basename"]. would simply take the image name and then rip off the .jpg portion. Instead what it's doing is only showing the first letter of the file and nothing else. Am I missing something or using basename incorrectly? Thanks!

Reply With Quote
  #2  
Old July 9th, 2012, 03:09 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,877 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 7 h 59 m 26 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
basename() is a function and $img2 is a string.

basename is the complement to dirname: the former will keep the file name (and extension) and discard the path while the latter will keep the path and discard the name.
PHP Code:
 $file "/path/to/file.ext";
echo 
basename($file); // file.ext
echo dirname($file); // /path/to

var_dump($file == dirname($file) . "/" basename($file)); // true 

PHP's basename() can also strip the file extension if you know what it is. Read the manual page to find out how.

Reply With Quote
  #3  
Old July 9th, 2012, 04:57 PM
poopertropper poopertropper is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 poopertropper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 31 sec
Reputation Power: 0
Thanks!!!

I went with

PHP Code:
<?php
     
foreach($allImgs2 as $img2){
     
$info pathinfo($img2);    
     
$file_name =  basename($img2,'.'.$info['extension']);
     echo 
'<li><a class="fancybox" href="'.$dir2.$img2.'" title="'.$file_name.'"><img src="'.$dir2.$img2.'"><h3>'.$file_name.'</h3></li></a>';
                        }
?>


This seemed to work ok. Not sure if it's bad coding practice but it did the job. Thanks so much!

Reply With Quote
  #4  
Old July 9th, 2012, 06:02 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,877 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 7 h 59 m 26 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
If you would care to take a closer look at pathinfo, you'll see that not only does it tell you the basename of the file, but also the basename without the extension. You don't need basename() at all if you're going that route.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Noob Question about basename

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap