keyboard
10-20-2011, 04:28 AM
Hello everyone,
I was browsing the internet and found this code
<?php
function RandomFile($folder='tmp', $extensions='txt'){
// fix path:
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;
// check folder:
if (!is_dir($folder)){ die('invalid folder given!'); }
// create files array
$files = array();
// open directory
if ($dir = @opendir($folder)){
// go trough all files:
while($file = readdir($dir)){
if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){
// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open the folder "'.$folder.'"');
}
if (count($files) == 0){
die('No files where found :-(');
}
// seed random function:
mt_srand((double)microtime()*1000000);
// get an random index:
$rand = mt_rand(0, count($files)-1);
// check again:
if (!isset($files[$rand])){
die('Array index was not found! very strange!');
}
// return the random file:
return $folder . $files[$rand];
}
include RandomFile('tmp/','txt');
I tried it ( the folder that the files are in is called tmp and they are all text files) but it came up with this
( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0007 380104 scandir ( ) ..\hi.txt:3
( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0007 380104 scandir ( ) ..\hi.txt:3
( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0007 380104 scandir ( ) ..\hi.txt:3
( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0012 380424 scandir ( ) ..\hi.txt:4
( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0012 380424 scandir ( ) ..\hi.txt:4
( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0012 380424 scandir ( ) ..\hi.txt:4
Scandir is the folder that the file is in (random.php).
Any help?
I was browsing the internet and found this code
<?php
function RandomFile($folder='tmp', $extensions='txt'){
// fix path:
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;
// check folder:
if (!is_dir($folder)){ die('invalid folder given!'); }
// create files array
$files = array();
// open directory
if ($dir = @opendir($folder)){
// go trough all files:
while($file = readdir($dir)){
if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){
// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open the folder "'.$folder.'"');
}
if (count($files) == 0){
die('No files where found :-(');
}
// seed random function:
mt_srand((double)microtime()*1000000);
// get an random index:
$rand = mt_rand(0, count($files)-1);
// check again:
if (!isset($files[$rand])){
die('Array index was not found! very strange!');
}
// return the random file:
return $folder . $files[$rand];
}
include RandomFile('tmp/','txt');
I tried it ( the folder that the files are in is called tmp and they are all text files) but it came up with this
( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0007 380104 scandir ( ) ..\hi.txt:3
( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0007 380104 scandir ( ) ..\hi.txt:3
( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 3
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0007 380104 scandir ( ) ..\hi.txt:3
( ! ) Warning: scandir(/tmp,/tmp) [function.scandir]: The system cannot find the file specified. (code: 2) in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0012 380424 scandir ( ) ..\hi.txt:4
( ! ) Warning: scandir(/tmp) [function.scandir]: failed to open dir: No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0012 380424 scandir ( ) ..\hi.txt:4
( ! ) Warning: scandir() [function.scandir]: (errno 2): No such file or directory in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt on line 4
Call Stack
# Time Memory Function Location
1 0.0004 375896 {main}( ) ..\random.php:0
2 0.0007 379976 include( 'C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\scandir\tmp\hi.txt' ) ..\random.php:52
3 0.0012 380424 scandir ( ) ..\hi.txt:4
Scandir is the folder that the file is in (random.php).
Any help?