The Student Room Group
Reply 1
You could just use explode with a . as the delimeter - this returns an array, and then you could just take the 0th value of this. This would work for variable length file extensions, but not for filenames with more than one . in them.
$filearr = explode(".",$fullfilename);
$filewithoutextension = $filearr[0];


Alternatively, if all your files have the same length file extensions (eg .doc .php .exe, not .html) you could use substr

$length=strlen($fullfilename); //gets the length of the full filename so you can tell substr when to stop
$filewithoutextension = substr($fullfilename,0,$fullfilename-4);
Reply 2
I would use:

substr - Return part of a string
strrpos - Find position of last occurrence of a char in a string

$filename = "myFile.php";
$pos = strrpos($filename, ".");
$myFilename = substr($filename,0,$pos);


note: this code isnt tested.
Reply 3
alex-hs
You could just use explode with a . as the delimeter - this returns an array, and then you could just take the 0th value of this. This would work for variable length file extensions, but not for filenames with more than one . in them.
$filearr = explode(".",$fullfilename);
$filewithoutextension = $filearr[0];


To get this fully working (working for filenames with multiple dots in the filename) check out the third parameter that [url=
http://www.php.net/explode]explode can take, and also look at implode:
$filearr = explode(".",$fullfilename,-1)
$filenamewithoutext = implode($filearr,".")


However, I'd go with your other solution but only when it works with file extensions of varying lengths, which you can do using strrpos:
$positionOfLastDot = strrpos($fullfilename,".")
$filenamewithoutext = substr($fullfilename,0,$positionOfLastDot)


All of this code assumes a dot exists, which may not necessarily be the case.
Reply 4
Thanks for the reply guys, i used the explode method and it works! :smile:

I am also trying to order the files by data modified or data added to the current directory (which i guess is date created), is there a function i can use to do this? Maybe like the sort() but for date created?

Thank you for anymore help. I really appreciate it.
Reply 5
You should check out PHP's documentation, it's good.

http://uk2.php.net/sort
Reply 6
abshirf2
I am also trying to order the files by data modified or data added to the current directory (which i guess is date created), is there a function i can use to do this? Maybe like the sort() but for date created?

Couldn't find one, so I wrote one. Feel free to use it.
function sortbyctime (&$array) {
$array = array_flip($array);
foreach ($array as $filename => &$number) {
$number = filectime($filename);
}
arsort($array);
$array = array_keys($array);
}
Reply 7
idiot
Couldn't find one, so I wrote one. Feel free to use it.
function sortbyctime (&$array) {
$array = array_flip($array);
foreach ($array as $filename => &$number) {
$number = filectime($filename);
}
arsort($array);
$array = array_keys($array);
}


I WILL USE IT! Thanks! :smile: (in return i will give you what little rep i have :wink:)
Reply 8
I have used the above code in my script but i would also like to echo the date the file was created. This is what i have so far:

<?php
function sortbyctime (&$array) {
$array = array_flip($array);
foreach ($array as $filename => &$number) {
$number = filectime($filename);
$date[] = $number;
}
arsort($array);
$array = array_keys($array);
}
$dir = 'Ogaden';
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$filename = $dir.'/'.$filename;
if(is_file($filename)){
$filename = basename($filename);
$files[] = $filename;
}
}//sort
sortbyctime($files);
for($i=0; $i<sizeof($files); $i++){
$names = explode(".", $files[$i]);
?>
<b><a href="http://www.ogaden.info/Ogaden-News.php?news=<?php echo $files[$i]; ?>"><?php echo $names[0]; echo $date[$i]; ?></a></b><br>
<?php } closedir($dh); ?>


The second last line is he problem. It doesn't echo out the date?!
Reply 9
<b><a href="http://www.ogaden.info/Ogaden-News.php?news=<?php echo $files[$i]; echo $names[0]; echo $date[$i]; ?></a></b><br>?
No, look at his code, he wants the filename and the date to be the link text.
Reply 11
Here's my rewrite of your code and the output it gave me on a test directory:
#!/usr/bin/php
<?php

function dir_list ($path) {
$array = scandir($path);
unset($array[0]); // remove .
unset($array[1]); // remove ..

$array = array_flip($array);
foreach ($array as $filename => &$number) {
$number = filectime($path .'/'. $filename);
}
arsort($array);

return $array;
}

$dir = 'exampledir';
$list = dir_list($dir);

foreach ($list as $filename => $epoch) {
if (!is_file($dir .'/'. $filename)) {
continue;
}
printf(
'<b><a href="http://www.ogaden.info/Ogaden-News.php?news=%s">%s (%s)</a></b><br />'."\n",
$dir .'/'. $filename,
substr($filename, 0, strrpos($filename, '.')),
gmdate('Y-m-d H:i:s', $epoch)
);
}

?>


<b><a href="http://www.ogaden.info/Ogaden-News.php?news=exampledir/newer.txt">newer (2007-07-27 14:27:09)</a></b><br />
<b><a href="http://www.ogaden.info/Ogaden-News.php?news=exampledir/file.txt">file (2007-07-27 14:24:18)</a></b><br />


It made sense to completely rewrite sortbyctime(). For one, it had a major bug: it didn't like paths. Also, it was throwing away that creation time data, which we want to keep. You try to keep it in your code with $date[] = $number;, but because it's in a function, that $date array is lost once the function ends, because it's not a global.

Also, you were trying to use the raw number output from filectime as a display date, which isn't going to look very nice on a webpage, because it's a UNIX timestamp. See the usage of gmdate() in my example above - making a nicer-looking date out of the timestamp.

And when you need to iterate over a bunch of arrays using a for loop, it's better to check the length once, in advance, rather than re-checking it every time the loop restarts. See http://www.php.lt/benchmark/phpbench.php
I'm talking about this:
for($i=0; $i<sizeof($files); $i++){
Reply 12
Thank you again. :smile: I have to wait 8 days so that i can rep you again, expect it then :wink:.

I've used your script and it works perfectly all i did was remove the date from the hyperlink and i dont remember variables anymore. You can see your work here: Ogaden News and Information. Lets not worry about the design, its supposed to be more functional than pretty. All the hyperlinks from the middle section is retrieved using your script. Except the first two.
Reply 13
Oops. filectime() has nothing to do with creation date. In fact, it's even less useful than file filemtime(). I don't think creation date is even *stored* anywhere, so if you really want that, you're going to have to go with some kind of hack, like embedding the creation date in the filename and extracting it for display.
Reply 14
idiot
Oops. filectime() has nothing to do with creation date. In fact, it's even less useful than file filemtime(). I don't think creation date is even *stored* anywhere, so if you really want that, you're going to have to go with some kind of hack, like embedding the creation date in the filename and extracting it for display.


Yeah, PHP is based on the UNIX system, so there is no such thing as created date. Only last accessed and modified. But it works for some reason?!

I must be lucky or i want it to be the "inode of change", i just dont know it. No idea But it seems to be doing the job!

I have been told to use somthing like FreeBSD by a friend but i havent got a clue on how! So i will get more info on that but for the time being its ok.