How do I paginate images with PHP? - php

I'm trying to paginate an arbitrary number of photos that I'm getting from a image directory onto my web page.
Here is my PHP script I'm running thus far:
<?php
$dir = "images/";
$counter = 1;
if($opendir = opendir($dir)){
//read directory
while(($file = readdir($opendir)) !== FALSE){
if($file != "." && $file != ".."){
echo "<img src='$dir/$file' alt='Picture broken' style='border-style: solid;border-width: 2px;border-color: #4d94ff; margin: 5px'>";
$counter++;
}
}
}
echo "<p>There are $counter images</p>";
?>
How do I get this to paginate automatically with 10 images on each page?

save all your images name of specific directory in a global array and paginate it with you own will :)

Related

Display latest image from directory

I use this to display the latest image from 2 directories where images from cameras upload every 10s
The code works, but as I know I might end up with potentially 10s of thousands of images in each directory I believe the code is not optimized for the situation.
Also I reload the whole page every 10s where maybe it would be more efficient to just update the images.
Could someone help giving me directions to optimize this?
Thanks a lot.
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
$base_url_east = 'East/snap/';
$base_url_south = 'South/snap/';
$newest_mtime_east = 0;
$show_file_east = 'BROKEN';
if ($handle = opendir($base_url_east)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
$mtime = filemtime("$base_url_east/$file");
if ($mtime > $newest_mtime_east) {
$newest_mtime_east = $mtime;
$show_file_east = "$base_url_east/$file";
}
}
}
}
$newest_mtime_south = 0;
$show_file_south = 'BROKEN';
if ($handle = opendir($base_url_south)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
$mtime = filemtime("$base_url_south/$file");
if ($mtime > $newest_mtime_south) {
$newest_mtime_south = $mtime;
$show_file_south = "$base_url_south/$file";
}
}
}
}
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body bgcolor="#000000">
<center>
<?php
print '<img src="' .$show_file_east. '" alt="Latest image uploaded" width="720" height="480">';
print '<img src="' .$show_file_south. '" alt="Latest image uploaded" width="720" height="480">';
?>
</center>
</body>
</html>
If the new files are named East/snap/current.jpg and South/snap/current.jpg, you could easily use the HTML even without PHP like this
<img src="East/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">
<img src="South/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">
The one/script uploads the photos should be responsible for
copying the already uploaded current.jpg to 2016-08-13-07:00:00.jpg (or some other filename containing eg. the date
copying the new file to current.jpg
EDIT
If you cannot modify the filenames like I said above, try
$string = date('Ymd-H'); // 20160812-12
$files = glob('Schedule_' . $string . '*.jpg');
This will get you all files into an array, which were taken in the current hour. It could be that you get a problem, if the script is called 9 seconds, after the hour changed, because it won't find an image from that hour. But with this way you can minimize the count of scanned files.
EDIT
This script is not tested, but should return one recent file for one folder:
<?php
$basePath = '/home/user/camera/East/snap';
// Scans files from the current and the last minutes
// this ensures that always files will be found, even if the minute changed
$date = new \DateTime();
$date->setTimeZone(new \DateTimeZone('America/Vancouver'));
$prefixCurrentMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$date->sub(new \DateInterval('PT1M'));
$prefixLastMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$files = array_merge(
glob($prefixLastMinute),
glob($prefixCurrentMinute)
);
$lastFile = 'dummy.jpg';
if (is_array($files) AND count($files) > 0) {
// this methods sorts the files "regularly" by default, and I guess
// regularly means alpha-numeric in ascending order ...
sort($files);
$lastFile = $files[0];
// use this, if the order is not ascending after using sort() on the array
// $lastFile = $files[count($files) - 1];
}
$eastPhoto = basename($lastFile);
?>
<img src="East/snap/<?php echo $eastPhoto; ?>" alt="Latest image uploaded" width="720" height="480">

PHP & HTML: Download file link not working

The following code lists all the files contained in a certain folder in my local machine, as you can see I'm echoing the file path inside the tag using the href attribute correctly.
So the problem is that when I click on the tag, it doesn't take me to the file download unless I copy the link and paste it to another tab of the browser, why is this happening? How can I fix it?
<h5>Attached Files</h5>
<?php
$date = $dateCreated->format('d-m-Y');
$thepath = public_path().'/uploads/binnacle/'.$date.'/'.$activity->activity_id;
$handle = opendir($thepath);
$x = 1;
while(($file = readdir($handle))!= FALSE) {
if($file != "." && $file != ".." && $file != "thumbs" && $file != "thumbs.db") {
echo $x.".- "."<a href='$thepath/$file' target='_blank'>$file</a><br>";
$x++;
}
}
closedir($handle);
?>
NOTE: This happens with all file types including images, excel files, text documents, etc.
SOLUTION BY #WereWolf - The Alpha:
<?php
$date = $dateCreated->format('d-m-Y');
$thepath = "/uploads/binnacle/".$date."/".$activity->activity_id;
$handle = opendir(public_path().$thepath);
$x = 1;
while(($file = readdir($handle))!= FALSE) {
if($file != "." && $file != ".." && $file != "thumbs" && $file != "thumbs.db")
{
echo $x.'.- '.''.$file.'<br>';
$x++;
}
}
closedir($handle);
?>
Make some changes:
// Remove the public_path()
$thepath = 'uploads/binnacle/'.$date.'/'.$activity->activity_id;
Then in the link:
"<a href='" . asset($thepath/$file) . "' target='_blank'>$file</a><br>";
Note: Laravel has a File component, You may use that, check it in the source.

Get images url from folder Filter only images from folder

I use the code suggested here
https://stackoverflow.com/a/18316453
This is what I have now.
<?php
$dir = "sliders/slides";
$images = array();
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if (!is_dir($dir.$file)) $images[] = $dir . '/' . $file;
}
closedir($dh);
}
}
echo json_encode($images);
?>
My result includes 2 extra items
sliders/slides/.
sliders/slides/..
which makes my slider having 2 extra blank slides
How can I filter the result to show only .jpg and .png files in order to remove /. and /.. be included in the results
I'm trying to create sliders that gets images from a folder
Thanks
Try something like this inside the while :
if ($file != "." && $file != ".." && !is_dir($file) {
$images[] = $dir . '/' . $file;
}

Easy way to sort getting images from a gallery using php?

Hello there good people of stackoverflow.
I have this code that basically get any photo i place in a folder called "pictures" and displays them on my page. Which is all good, and it works, with lightbox as well.
My main question is, can there be an easy way to display the photos in some form of order? i.e newest photos first?
<?php $handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){if($file !== '.' && $file !== '..'){
echo '<img src="pictures/'.$file.'" border="0" />';}}?>
I know the code is very vulgar and ancient, but its only for a sample page so it doesn't need to be massive.
You can order them using php's filemtime(): http://php.net/manual/en/function.filemtime.php
<?php
$path = dirname(realpath(__FILE__)).'/pictures/';
$handle = opendir($path);
$arrFiles = array();
while($file = readdir($handle))
{
if($file !== '.' && $file !== '..')
{
$arrFiles[filemtime($path.$file)] = '<img src="pictures/'.$file.'" border="0" />';
}
}
arsort($arrFiles);
foreach ($arrFiles as $file)
{
echo $file;
}
?>

php grab images from directory

I have this PHP script which i'm grabbing images from a directory and displaying them. The directory only has 4 image files in it and yet there are 6 li's. In firebug the images just have the paths 'Images/uploaded/.' and 'Images/uploaded/..'
Are there hidden files that this script is grabbing but not displaying correctly?
<?php
$dir = 'Images/uploaded/';
if($handle = opendir($dir)) {
while(false !== ($file = readdir($handle))) {
echo "<li><img class=\"thumb\" src=\"".$dir.$file."\" /></li>";
}
}
closedir($handle);
?>
Your test for . or .. fails... example: You load '.' and your test says:
if('." is not "." OR its not "..")
change this conditional to && (and).
Those are the entries for "current directory" and "parent directory, respectively. Just filter them out.

Categories