|
-
Senior Member
[php] Finding multiple files
If i have a group of txt files with the names from 0000 upwards, how can i check to see which txt file is the highest? (Without knowing it prior)
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
Registered User
hi,
One solution:
Store the text files in an array, order them, and get the last one
PHP Code:
<?php
$myArray = array('0000.txt', '0002.txt', '0001.txt', '0060.txt', '0023.txt', '0030.txt');
// sort the array
sort ($myArray);
print_r ($myArray);
echo '<br />';
// go to the last element
$last = end ($myArray);
echo 'the last element is: ' . $last;
?>
-
Senior Member
Thank you for spending the time to reply but it wasnt quite what i needed.
I have a folder that has txt files in it. I know that they are numbered from 0000 onwards. I dont know how many there are.
I would like to know how i can run a loop to find all of the files and put them into an array. The reason is that is that i dont want to have to upload a new php file everytime i create a new txt file, i would like to automate the finding and putting into array process.
Will there be seriouse slowdown once there is 1000+ txt files to add to the array?
I was wondering if maby it would be faster if i saved the array to another txt file and just use that as a referance (IF this is possable) so that it wouldnt have to calculate a new array every time i needed to find a certain file.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
Registered User
Reading the content of the directory is easy: readdir()
The logic is: read the content. See if the extension of the file is .txt, in which case you'll add it to the array.
I'm not sure about the slowdown of reading 1000+ files, but I'd say it's not a good idea. When you add a new file, can't you remove the previous one?
With so many files, I suggest you consider using a database.
-
Senior Member
With so many files, I suggest you consider using a database.
I strongly doubt i will get to 1000+ files, hopfully by then i will have learnt to use a database with php.
I have an idea but because im not fluent enough to write php without looking at references resulting a good 1 hour to rite a few lines of code so would you be able to help me translate my AS code into php?
Code:
var i = 1
var fileID = 0000
filearray = new Array();
if ("fileID".txt hasn been created){
// create it
filearray [fileID]
}
else { filearray [fileID] }
//When this loops runs it makes fileID go up by one, now maing it 0001
for (fileID=0000; fileID<i; fileID++) {
if (fileID.txt exists) {
filearray [fileID]
i++
}
}
I hope you are ok with AS, i did this just then so it might not be %100 right but i hope you get the hole idea of how i want to be able to add the files to an array.
Thanks
EDIT : PS: Also thanks for a fast reply
EDIT2: Just realised i could have created that for loop as a while loop. I will edit after next reply
Last edited by Gloo pot; 07-04-2006 at 10:11 AM.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
I use this script to loop through all files that have the .txt extension and count them. Maybe this is of use to you...
PHP Code:
<?php
// count.php count all text files
$count = 0;
// read current dir
$handle=opendir('./');
// loop through files in $handle dir
while ($file = readdir($handle)) {
// check extension
if ( strtolower( substr( $file, -4 ) ) == ".txt" ){
// increment counter
$count++;
}
}
// close dir
closedir($handle);
?>
HTH
// Kind regards, Danielle.
// specs: Flash CS5.5 | Flash Builder 4.6 | win xp pro
-
Registered User
Using the code I provided before, readdir, and the idea I gave you:
PHP Code:
<?php
$myArray = array (); // create new array
if ($handle = opendir('./textfilesfolder/')) { // change the dir here
while (false !== ($file = readdir($handle))) {
if (strpos ($file, '.txt') !== false) // if $file contains .txt
{
array_push ($myArray, $file); // add to the array
}
}
closedir($handle);
}
// sort the array
sort ($myArray);
print_r ($myArray);
echo '<br />';
// go to the last element
$last = end ($myArray);
echo 'the last element is: ' . $last;
?>
Your logic may be valid too, but it doesn't account for missing files (if 0004.txt is missing, 0003.txt is the last, although 0005.txt may exist).
And in order to have 0001.txt, 0002.txt, ... I'd have to write a function to add the extra 0's. This wouldn't be needed if the files were 1.txt, 2.txt, 3.txt, ...
-
Registered User
hi Danielle R,
From the php documentation:
PHP Code:
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
but I like this piece, which ensures the extension is .txt (and I was too lazy to write):
PHP Code:
if ( strtolower( substr( $file, -4 ) ) == ".txt" ){
}
although the variable $count appears out of context.
-
Senior Member
Thanks heeps, im just going to run over your code and comment on how i assume it works, just so i know whats going on, i hate having code that i dont understand it just makes learning a new programming languages so much harder.
PHP Code:
<?php
// Create New Array
$myArray = array ();
// It checks to see if we can open to folder?
if ($handle = opendir('.//*Would be my own folder*//')) {
// Dont know what this while loop does
// Can you run it by me with a little more detail?
while (false !== ($file = readdir($handle))) {
// Whats strpos?
// Checks to see if the file has .txt in it
if (strpos ($file, '.txt') !== false)
{
// add a new place to the array and fills it will the file name?
array_push ($myArray, $file);
}
}
/// Closes $handle which i assume was a varible which held the pointer to the folder we are using?
closedir($handle);
}
// How is it sorted? Alphabeticly?
sort ($myArray);
// What does this mean?
print_r ($myArray);
// Add a new line on the page... Why?
echo '<br />';
// Sets $last to the last slot in $myArray
$last = end ($myArray);
// Prints the last slot in myArray
echo 'the last element is: ' . $last;
?>
Thanks for helping me... Hopfully after this i will have preaty much everything i need. Im off to bed so i wont reply for about 12 hours...
Thanks again.
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
-
Registered User
I'd say that the PHP manual answers all your questions.
Just search each of the functions (in the search for box). If you still have problems after this, please feel free to ask again!
-
Senior Member
Thank you...
92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|