abshirf2
Badges: 1
Rep:
?
#1
Report Thread starter 12 years ago
#1
Hello all,

I think what i want to do is string patterns in PHP or regular expressions?!

I have file names that end in doc, like work.doc, abshirf2.doc etc. I just want the fileaname not the extension, how can i do this?

Thanks all
0
reply
alex-hs
Badges: 2
Rep:
?
#2
Report 12 years ago
#2
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.
PHP Code:
$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
PHP Code:
$length=strlen($fullfilename); //gets the length of the full filename so you can tell substr when to stop
$filewithoutextension substr($fullfilename,0,$fullfilename-4); 
0
reply
*Mike*
Badges: 1
Rep:
?
#3
Report 12 years ago
#3
I would use:

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

PHP Code:
$filename "myFile.php";
$pos strrpos($filename".");
$myFilename substr($filename,0,$pos); 
note: this code isnt tested.
0
reply
TomX
Badges: 14
Rep:
?
#4
Report 12 years ago
#4
(Original post by 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.
PHP Code:
$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 explode can take, and also look at implode:
PHP Code:
$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:
PHP Code:
$positionOfLastDot strrpos($fullfilename,".")
$filenamewithoutext substr($fullfilename,0,$positionOfLastDot
All of this code assumes a dot exists, which may not necessarily be the case.
0
reply
abshirf2
Badges: 1
Rep:
?
#5
Report Thread starter 12 years ago
#5
Thanks for the reply guys, i used the explode method and it works!

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.
0
reply
TomX
Badges: 14
Rep:
?
#6
Report 12 years ago
#6
You should check out PHP's documentation, it's good.

http://uk2.php.net/sort
0
reply
idiot
Badges: 1
Rep:
?
#7
Report 12 years ago
#7
(Original post by 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.
PHP Code:
function sortbyctime (&$array) {
    
$array array_flip($array);
    foreach (
$array as $filename => &$number) {
        
$number filectime($filename);
    }
    
arsort($array);
    
$array array_keys($array);

0
reply
abshirf2
Badges: 1
Rep:
?
#8
Report Thread starter 12 years ago
#8
(Original post by idiot)
Couldn't find one, so I wrote one. Feel free to use it.
PHP Code:
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! (in return i will give you what little rep i have )
0
reply
abshirf2
Badges: 1
Rep:
?
#9
Report Thread starter 12 years ago
#9
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 Code:
<?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?!
0
reply
JamieB
Badges: 0
Rep:
?
#10
Report 12 years ago
#10
<b><a href="http://www.ogaden.info/Ogaden-News.php?news=<?php echo $files[$i]; echo $names[0]; echo $date[$i]; ?></a></b><br>?
0
reply
guitarromantic
Badges: 1
Rep:
?
#11
Report 12 years ago
#11
No, look at his code, he wants the filename and the date to be the link text.
0
reply
idiot
Badges: 1
Rep:
?
#12
Report 12 years ago
#12
Here's my rewrite of your code and the output it gave me on a test directory:
PHP Code:
#!/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($filename0strrpos($filename'.')),
        
gmdate('Y-m-d H[s]i[/s]s'$epoch)
    );
}

?>
Code:
<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:
PHP Code:
for($i=0$i<sizeof($files); $i++){ 
0
reply
abshirf2
Badges: 1
Rep:
?
#13
Report Thread starter 12 years ago
#13
Thank you again. I have to wait 8 days so that i can rep you again, expect it then .

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.
0
reply
idiot
Badges: 1
Rep:
?
#14
Report 12 years ago
#14
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.
0
reply
abshirf2
Badges: 1
Rep:
?
#15
Report Thread starter 12 years ago
#15
(Original post by 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.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you registered to vote?

18-20 years old (yes) (261)
54.38%
18-20 years old (no) (62)
12.92%
20-25 years old (yes) (80)
16.67%
20-25 years old (no) (9)
1.88%
25-30 years old (yes) (25)
5.21%
25-30 years old (no) (0)
0%
30-40 years old (yes) (24)
5%
30-40 years old (no) (3)
0.63%
40+ years old (yes) (9)
1.88%
40+ years old (no) (7)
1.46%

Watched Threads

View All
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise