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 Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/ 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] Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143789 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143794 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 Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143798 Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 [code]$ext = strrchr($filename,'.');[/code]Ray Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143803 Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 thanks. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143804 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143806 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143809 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] Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143814 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143817 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143819 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? Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143820 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] Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143821 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 Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143828 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] Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143831 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143832 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143835 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. Link to comment https://forums.phpfreaks.com/topic/31135-seperate-file-extension-from-file-name/#findComment-143847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.