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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.