分页和可点击的图像

I want to make an image gallery that has several pages. When a user logs in i want them to be able to upload an image, view it, and see it on the home page.

The user should also be able to browse other images that other users posted. I'm all set with the user system and will modify it later but the gallery and php pagination system is killing me at the moment.

I've used previous codes found online to display the image files in my directory but so far this is the only code that displays the latest images uploaded. All images are being uploaded to the folder called "uploaded".

Using dreamweaver and the error logs are:

Notice: Undefined variable: opendir in C:\Users\User\Documents\Xampp\htdocs\HT\index.php on line 186

Warning: readdir() expects parameter 1 to be resource, null given in C:\Users\User\Documents\Xampp\htdocs\HT\index.php on line 186

Notice: Undefined variable: file in C:\Users\User\Documents\Xampp\htdocs\HT\index.php on line 187

Notice: Undefined index: page in C:\Users\User\Documents\Xampp\htdocs\HT\index.php on line 197

The images are showing, and the pagination has the "First,previous,next,and Last" but the images are not clickable and when i tried directing them with $dir variable it sent me to an error page and not the image which i clicked.

The issue with the pagination is that when i click either "first,previous...bla bla bla" it sends me to the xampp "Index of /.../uploaded"

<?php
$dir = readdir($opendir);
$opendir = opendir($file);
$file = ('uploaded/');

$files = glob("uploaded/*.*");
usort($files, function ($a, $b) {
return filemtime($b) - filemtime($a);
});

$record_count  = 20;
$totla_pages   = ceil(count($files)/$record_count);
$page = $_REQUEST['page']; ///make it dyanamic :: page num
$offset        = ($page-1)*$record_count;
$files_filter  = array_slice($files, $offset,$record_count);

foreach ($files_filter as $file) {
echo "<a href='$dir/$file'><img src='$file' style='height:180px;width:180px; border:2px solid black;  margin:20px 0px 10px  10px; *margin:10px 0px 10px 20px;'></a>";
}

if($totla_pages > 1){
   if($page != 1){
      echo '<a href="thispage.php?page='.($page-1).'">Prev</a>';
   }
   if($page != $totla_pages){
      echo '<a href="thispage.php?page='.($page+1).'">Next</a>';
   }
}
?>

$opendir hasn't been defined, so that's why you're getting a warning right away, which then leads to the rest of the errors.

You also don't need the readdir() function, only the opendir() function will suffice.

You can replace:

$dir = $opendir($opendir); 
$opendir = opendir($file); 
$file = ('uploaded/');

with:

$file = "uploaded"; 
$dir = "."; 
opendir($dir);

The dot in $dir = "."; is for the current folder it's in; that needs to remain like that.


Edit:

To fix the "undefined index: page" notice as soon as the initial page loads:

You can get rid of $page = $_REQUEST['page']; and then use this:

$record_count  = 20;
$totla_pages   = ceil(count($files)/$record_count);
// $page = $_REQUEST['page']; // no longer needed

$home_script = $_SERVER['REQUEST_URI'];
    if($_SERVER['REQUEST_URI'] == $home_script) {
      $page = "";
}

$offset = ($page-1)*$record_count;
$files_filter  = array_slice($files, $offset,$record_count);