Jump to content


Photo

Simple problem (i think)


  • Please log in to reply
7 replies to this topic

#1 Exussum

Exussum
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 15 August 2006 - 07:22 PM

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

#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 15 August 2006 - 08:14 PM

Don't know but see what happens if you change the line
if($ll = 'g'){

to

if($ll == 'g'){

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 Exussum

Exussum
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 15 August 2006 - 08:31 PM

same thing - it  woudnt make too much differnce atm because the pics are all jpegs atm

Thanks for the reply though

#4 Exussum

Exussum
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 16 August 2006 - 08:58 PM

Sorry for the Bump - but has any one got any ideas ?

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 16 August 2006 - 09:10 PM

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
if ($file{0} != '.')

is correct? Did you not mean

if ($file[0] != '.')

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 Exussum

Exussum
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 16 August 2006 - 09:17 PM

King arther - i corrected that since i posted - and the second bit means the First letter not the first part of the array - i get the same error though :(

#7 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 16 August 2006 - 10:42 PM

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#8 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 17 August 2006 - 12:55 AM

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.

<?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]);
	}
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users