PDA

View Full Version : Resolved php random file include, can't find file



keyboard
10-20-2011, 05: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?

TechKat
10-20-2011, 11:17 PM
Get rid of =='tmp/' and =='txt'

Should work

keyboard
10-21-2011, 12:41 AM
Do you mean it should look like this


<?php
function RandomFile(){

// 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');

traq
10-21-2011, 01:55 AM
The code you posted does not use the scandir() function, but the error messages you provide are all related to scandir().

Obviously, this is either not the output from the code you posted, or there is more to the script than you have posted here. Could you please double-check, and post your entire code?


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?

keyboard
10-21-2011, 02:40 AM
I worked out what was wrong. In the file hi.txt I had accidently put php code instead of normal text. because it was including it into the page it was executing the php code and stuffing the whole thing up. Thanks Traq, you were really helpfull

djr33
10-21-2011, 04:02 AM
Don't include text like that. Use file_get_contents() to save the values to a string. As you've now learned, using include() can be dangerous.

traq
10-21-2011, 05:20 AM
...especially when you're finding the file dynamically.

keyboard
10-21-2011, 05:26 AM
Thanks for all your help guys!!!