Jump to content

[SOLVED] Image Retreival


think-digitally

Recommended Posts

Hi. I am trying to retrieve an image from a folder on the server, each time the page is refreshed a random image from the folder appears.

 

Here is what I have got so far, and well its not working, and I am swimming far out at sea...

 

Would be greatful to anyone that could give some help.

 

  <?php
$dir = "/frontpage/";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
	echo "<img src=".$file." />" . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/110786-solved-image-retreival/
Share on other sites

Try this:

 

<?php

function random_file ($dirname)
{
if(!is_dir($dirname))
	return false;
if(!$h = opendir($dirname))
	return false;

$files = array();
while($file = readdir($h) !== FALSE)
	if(!is_dir($file))
		$files[] = $file;

return $files[array_rand($files)];
}


$dir = "/frontpage/";
$img = random_file($dir);
if($img !== false)
echo "<img src=".$img." />" . filetype($dir.$img) . "\n";

?>

 

 

Orio.

You could just emulate the scandir function in his code from earlier...

 


$dir = "/frontpage/";

if(!function_exists('scandir')) {
   function scandir($dir) {
       $d = opendir($dir);
       $r = array();
       while(false !== ($f = readdir($d))) {
           $r[] = $f;
       }
       return $r;
   }
}

$files = scandir($dir);
$rand = rand() % (count($files) - 2) + 2;
echo "<img src=".$files[$rand]." />" . filetype($dir . $file[$rand]) . "\n";

Well crap.... Editing the old post is breaking the HTML.... I guess I'll repost it... Sorry for the multiple posting x.x.

 

<?php

$dir = "/frontpage/";

if(!function_exists('scandir')) {
    function scandir($dir) {
        $d = opendir($dir);
        $r = array();
        while(false !== ($f = readdir($d))) {
            $r[] = $f;
        }
        return $r;
    }
}

$files = scandir($dir);
$rand = rand() % (count($files) - 2) + 2;
echo "<img src=".$files[$rand]." />" . filetype($dir . $file[$rand]) . "\n";

?>

No problem, thanks for helping

 

I am afraid there are errors

 

Warning: opendir(/2/): failed to open dir: No such file or directory in /home/content/a/1/6/a16538296/html/index1.php on line 12

 

Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/a/1/6/a16538296/html/index1.php on line 14

 

Warning: filetype(): Lstat failed for (null) (errno=2 - No such file or directory) in /home/content/a/1/6/a16538296/html/index1.php on line 23

 

I have checked the name of the folder, and its defo correct, so not sure

If it were correct, you wouldn't be getting errors ;p....

 

Well, you might still.... Does PHP have read access to the target directory?

 

Also, /2/ means the directory 2 from the root of the drive....  Do you mean the root of the drive, or do you want PHP to access a directory a level below the php file?  If so, it needs to be 2/, not /2/.

 

 

Couldn't you do like:

<?php
$top=10; //How many images you have... or anything I guess.
$dir="/dir/";
$od=opendir($dir);
$rand=rand(1,$top);
$done=false;

while (!$done) {
  while ($file=readdir($od)) {
    $nr=rand(1,$top);
    if ($nr==$rand) {
      $done=true;
      header("Content-type image/".filetype($dir."".$file));
      readfile($dir."".$file);
    }
  }
}

closedir($od);
?>

 

Not tested :P. I hope it works, I'm kind of tired right now though.

Well

<?php
$top=6; //How many images you have... or anything I guess.
$dir="/dir/";
$od=opendir($dir);
$rand=rand(1,$top);
$ndone=true;

while ($ndone) {
  while ($file=readdir($od)) {
    $nr=rand(1,$top);
    if ($nr==$rand) {
      readfile($dir."".$file);
      $ndone=false;
    }
  }
}

closedir($od);
?>

 

Grabs a random file and reads the contents. Problem is, I can't get the header to work (above readfile) so it can display as an image :/.

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.