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

You want to display a random image from that folder?

 

<?php

$dir = "/frontpage/";

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

?>

 

Orio.

Link to comment
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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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";

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :/.

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.