Jump to content

Simple problem (i think)


Exussum

Recommended Posts

Hey - aint used PHP in a few months and now a simple script wont work for me


<?php
function dirlisting($folder){ //set a function
if ($handle = opendir($folder)) { // open the Folder
$num = 0;
while (false !== ($file = readdir($handle))) { // Make an array
if ($file{0} != '.') { //if the file dont start with a "."
$files[$num] = $file;
$num++;
}//End if
}// End While
}  // End open
$random= rand(0, (count($files)-1));
$dir ='images/'; // where the images
$ll = substr($files[$random], -1);
if($ll = 'g'){
header ('Content-type: image/jpeg');
include ($dir . $files[$num]);
}else{
header ('Content-type: image/gif');
@include ($dir . $files[$num]);
//header ("Location: " . $dir . $files[$random]);
}//end else
}//end function
dirlisting($folder){ //use function
?>

and this error comes up

<br />
<b>Warning</b>:  main(images/) [<a href='function.main'>function.main</a>]: failed to open stream: No such file or directory in <b>/home/exussum/public_html/sig/index.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>:  main(images/) [<a href='function.main'>function.main</a>]: failed to open stream: No such file or directory in <b>/home/exussum/public_html/sig/index.php</b> on line <b>16</b><br />

<br />
<b>Warning</b>:  main() [<a href='function.include'>function.include</a>]: Failed opening 'images/' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in <b>/home/exussum/public_html/sig/index.php</b> on line <b>16</b><br />


Any ideas on how to fix ?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/17665-simple-problem-i-think/
Share on other sites

Well, it looks as if in your first while loop you are incrementing $num past the end of the array $files[] but then you are using $files[$num] later, which would give an empty string.

Also, not sure if this
[code]
if ($file{0} != '.')
[/code]

is correct? Did you not mean

[code]
if ($file[0] != '.')
[/code]
Link to comment
https://forums.phpfreaks.com/topic/17665-simple-problem-i-think/#findComment-75879
Share on other sites

Best post the code you have now then, also what is the value being passed in and what are the contents of that directory? The only other thing I can think of would be that the while loop is not executing at all therefore the array element $files[$num] is still an empty string.
Link to comment
https://forums.phpfreaks.com/topic/17665-simple-problem-i-think/#findComment-75938
Share on other sites

Why are you setting $dir to be something other than $folder?  Also, I wouldn't include the image file.  If you are returning an image to the browser, then just use file_get_contents and echo out the file.

[code]<?php
function dirlisting($folder){
if ($handle = opendir($folder)) {
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file{0} != '.') {
$files[$num] = $file;
$num++;
}
}
}

$random = rand(0, (count($files)-1));
$ll = substr($files[$random], -1);

if (substr($folder, -1) != "\\") {
$folder .= "\\";
}

if ($ll == 'g'){
header ('Content-type: image/jpeg');
echo file_get_contents($folder . $files[$num]);
} else {
header ('Content-type: image/gif');
echo file_get_contents($folder . $files[$num]);
}
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/17665-simple-problem-i-think/#findComment-75985
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.