Jump to content

seperate file extension from file name


ted_chou12

Recommended Posts

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 total
echo "$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.
Give this a try.
[code]
<?php
function 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 total
echo "$total\n";
?>[/code]
This:
[code]<?php
function 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 total
echo "$total\n";
?>[/code]
nope this time it truely returns 0.
here, i will give you examples:
topic1-replies.php
topic2-replies.php
topic3-replies.php
...
topic99-replies.php
topic100-replies.php
...
topic999-replies.php
topic1000-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
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 total
echo "$total\n";
?>[/code]
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? ;D
T.T.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.