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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.