ted_chou12 Posted December 18, 2006 Share Posted December 18, 2006 i believe someone posted this already, but i cant seem to find it, if not, can anyone tell me how to separate file extension from file name, (i want the file extension).ThanksTed Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 18, 2006 Share Posted December 18, 2006 I think that was me that posted it, but anyways here is what I came up with:[code]<?phpfunction getext($filename) { $pos = strrpos($filename,'.'); $str = substr($filename, $pos); return $str;}echo getext("thisismyfile.html");?>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 sorry, but can you explain which one gives you the file extension?thankyouTed. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 nope, i dont think this getext() works, i cant find it on php manual as well. :'(Ted Quote Link to comment Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 [code]$ext = strrchr($filename,'.');[/code]Ray Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 thanks. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 18, 2006 Share Posted December 18, 2006 the getext isn't in the manual, because it is a function that I created, it works for me thouhgh, but maybe it did something that you didn't want, idk. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 but why cant i get it to work on here? it just doent show up anything at all![code]$dir = "homework/";// Open the directory$dh = opendir($dir);while (($file = readdir($dh)) !== false){$ext = strrchr($filename,'.');//here i want to know if it is txt file or not, if not, i dont want to get it to read.if ($ext == ".txt") { $fullfile = $dir . $file; $subtotals[] = file_get_contents($fullfile);}}// Add them all together$total = 0;foreach ($subtotals as $st){ $total = $total + $st;}// Echo the totalecho "$total\n";[/code]oh, so you have 'function' at the front..., but i dont know why it doesnt work for me, still, a big thanks to TLG. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 18, 2006 Share Posted December 18, 2006 Give this a try.[code]<?phpfunction getext($filename) { $pos = strrpos($filename,'.'); $str = substr($filename, $pos); return $str;}$dir = "homework/";// Open the directory$dh = opendir($dir);while (($file = readdir($dh)) !== false){$ext = getext($filename);//here i want to know if it is txt file or not, if not, i dont want to get it to read.echo "$ext";if ($ext == ".txt") { $fullfile = $dir . $file; $subtotals[] = file_get_contents($fullfile);}}// Add them all together$total = 0;foreach ($subtotals as $st){ $total = $total + $st;}// Echo the totalecho "$total\n";?>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 nope, i tried it, and it returns me 0, which is not the real figure. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 18, 2006 Share Posted December 18, 2006 it doesn't look like you are defining $filename. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 hey sorry, my mistake, the filename is "topic101-replies.php" however, the number "101" in which may variey. What i want is that the function recognizes this format of file name, you understand me? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 18, 2006 Share Posted December 18, 2006 This:[code]<?phpfunction getext($file) { $pos = strrpos($file,'.'); $str = substr($file, $pos); return $str;}$dir = "homework/";$filename = "topic101-replies.php"; #This is where you will change the name of the file.// Open the directory$dh = opendir($dir);while (($file = readdir($dh)) !== false){$ext = getext($filename);//here i want to know if it is txt file or not, if not, i dont want to get it to read.echo "$ext";if ($ext == ".txt") { $fullfile = $dir . $file; $subtotals[] = file_get_contents($fullfile);}}// Add them all together$total = 0;foreach ($subtotals as $st){ $total = $total + $st;}// Echo the totalecho "$total\n";?>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 nope this time it truely returns 0.here, i will give you examples:topic1-replies.phptopic2-replies.phptopic3-replies.php...topic99-replies.phptopic100-replies.php...topic999-replies.phptopic1000-replies.php...goes on forever......but whatever the number is, i want it to recognize as "topic"..."-replies.php"yeah.fully understood?if not, i will further explain...Ted Quote Link to comment Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 need to change $filename to $file[code]<?php$dir = "homework/";// Open the directory$dh = opendir($dir);while (($file = readdir($dh)) !== false){$ext = strrchr($file,'.');//here i want to know if it is txt file or not, if not, i dont want to get it to read.if ($ext == ".txt") { $fullfile = $dir . $file; $subtotals[] = file_get_contents($fullfile);}}// Add them all together$total = 0;foreach ($subtotals as $st){ $total = $total + $st;}// Echo the totalecho "$total\n";?>[/code] Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 18, 2006 Share Posted December 18, 2006 so what your saying is you don't want the file extension, you want the number within the filename. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 um... I want the function to be able to recognize the format of the filename, not the extension,(sorry it was my bad at first), so that it only sees the replies counter files. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 nevermind, thanks for all of you, I was being clever, give the counter files a new extension, dont bother with the filename format and stuff, they are just too complictated, sometimes we have to think smarter, dont we? ;DT.T. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.