I'm trying to build a gallery for a friend, using PHP. Currently my script imports all the images from a "gallery" folder, and it displays them alphabetically, using automatically generated thumbnails and fancybox plugin.
Is it posible to sort them by date? It doesn't matter if it's the date when they were taken or the date when they were last modified. The code I use is below. Thanks in advance!
<?php
$path = 'gallery/';
$files = scandir('gallery/');
?>
<ul>
<?php foreach ($files as $file){
if ($file == '.' || $file == '..'){
echo '';
} else {
?>
<li><a class="fancybox" rel="group" href="<?php echo $path . $file; ?>"><img src="scripts/timthumb.php?src=<?php echo $path . $file; ?>&h=194&w=224&zc=1&q=100" /></a></li>
<?php } }?>
</ul>
this php function sorts your file by the last date it was modified.
Don't forget to put in the ignored files array which files you want to be ignored.
function scan_dir($dir) {
$ignored_files = array()
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file,$ignored_files) {
$files[$file] = filemtime($dir.'/'.$file);
}
}
arsort($files);
$files = array_keys($files);
if(is_null($files))
return false;
return $files;
}
You can refactor it a bit this was made really quicly. Hope this will work
Related
I have a PHP script that scans a specified Movie directory then displays it styled on a webpage using for loop and php. The code is below. I tried using glob but once I have all the images in an array how do I compare them to the array with all of the movies then if the image and the movie folder match to display the image with the correct name?
<?php
// set dir to the directiry you want to index
$dir = 'Movies/';
// scanning the directory you set
$scan = scandir($dir);
// I have this value at 11 because the first movie shows up
// in the 11th file in the array
$file = 11;
// This then removes the first useless files from our array
$scanned = array_slice($scan, $file);
// gets the amount of files
$FileNum = count($scanned);
// display all images and fanart
$images = glob('*.jpg');
// for loop that goes through the array
for ($i = 0; $i <= $FileNum; $i++) {
// gives the class for styling
echo '<li class="image">';
this is problem bit
// check if there is fanart/images in the folders
if (file_exists($dir . $scanned[$i] . $images)) {
// if there is images display them styled
echo '<img id="box1" src="' . $dir . $scanned[$i] . '*.jpg' . '" width="280" height="150" />';
} else {
// if not then display default image
echo '<img id="box1" src="http://placehold.it/280x150" width="280" height="150" />';
}
// make the box clickable to where the folder is located
echo '<a href="'. $dir . $scanned[$i] .'">';
// display the name of the movie and some JS
echo '<span class="text-content"><span>' . $scanned[$i] .'<br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br><i class="fa fa-chevron-down" onclick="openNav()" aria-hidden="true"></i></span></span> </a>';
}
The file structure is as follows
`MOVIES---
\--random movies
\--mp4 and .jpg files`
To clarify my question is - Is there a way to check if a file exists, and if it does then put it in an array? I've tried using glob but that can't check if the file exists.
Well there is an * in your echo i don't think it needs to be there.
And if your movie directory gets updated, or the images. Then your script does not work anymore. Because of the hard slicing (11th file).
Maybe this will work for you:
<?php
// movies
$dir = "movies/";
$files = scandir($dir);
$movies = array();
$images = array();
foreach ($files as $file) {
// check for the mime type:
$mime = mime_content_type($dir . $file);
$type = substr($mime, 0,5);
$filename = pathinfo($dir . $file, PATHINFO_FILENAME);
if ($type == "video") $movies[] = $file;
if ($type == "image") $images[] = $filename;
}
foreach ($movies as $movie) {
$placeholder = true;
foreach($images as $image) {
if (strpos($movie, $image) !== false) {
$placeholder = false;
continue;
}
}
if ($placeholder) {
echo $movie . " - placeholder<br>";
} else {
echo $movie . " - image<br>";
}
}
It works with the mime-type.
<?php
$dir_path = "./folder/";
if(is_dir($dir_path))
{
$files = opendir($dir_path);
{
if($files)
{
while (($file_name = readdir($files)) !== FALSE)
{
if ($file_name != '.' && $file_name != '..'){
echo "".$file_name."<br>";
#echo "<img src=".$file_name.">";
}
}
}
}
}
?>
Returns an array of files and directories from the directory . ... I wanted to easely access data in a certain directory using foreach. I came up with the following:
but it is not download
it say like this
Object not found!
In these cases first you need get files of directory like following:
$dir = './FILE_FOLDER_NAME';
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
Why we used unset, these code remove . and .. from $files variable and you have just file names.
Now you can show files with this approach:
foreach($files as $key => $value):
$path_info = pathinfo($value); //RETURN FILE EXTENTION
?>
<a href="DIRECTORY_PATH<?php echo $value; ?>" target="_blank"><?php echo $value; ?>
<?php
endforeach;
If you want to delete a file can add new button to your foreach like this:
<button href="PHPSAMPLEFILE.PHP?file=<?php echo base64_encode ($value); ?>"><?php echo 'DELETE'; ?></button>
and in your PHP file:
$file = base64_decode($_GET['file']);
$path = './DIRECTORY_PATH/'.$file;
unlink($path);
I have the following code for an image gallery :
$directory = 'some path';
$thumbs_directory = 'some path';
foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file)
foreach (glob($thumbs_directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file2)
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = basename($file);
$title = htmlspecialchars($title);
$title = str_replace("_"," ",$title);
$nomargin='';
if(($i+1)%4==0) $nomargin='nomargin';
echo '
<div class="pic '.$nomargin.'" style="background:url('.$file2.') no-repeat 50% 50%;">
'.$title.'
</div>';
$i++;
}
I need to combine these foreach statements through the Logical AND operator && so that both conditions are satisfied at the same time. Is it possible ? I've tried numerous times, but always ends up in a Syntax error.
Please note that I need $file and $file2 variables defined perfectly. That is only the way for the thumbnails to associate with the images properly.
Can you not simply refactor the meaty logic into a common function, and then call it twice?
For example:
function doSomething($directory) {
foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file) {
/* Whatever */
}
}
...
doSomething($directory);
doSomething($thumbs_directory);
Based on your description, if you want to loop over the image files that exist in both directories, you should look into using php's array_intersect().
$directory = 'some path';
$thumbs_directory = 'some path';
$files_in_dir1 = glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$files_in_dir2 = glob($thumbs_directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$files_in_both_dirs = array_intersect($files_in_dir1, $files_in_dir2);
foreach ($files_in_both_dirs as $filename) {
// Code
}
To map images to their respective thumbnail images, I would rather choose a different approach:
$directory = 'some path';
$thumbs_directory = 'some path';
// Get all images
$images = glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
// Iterate over all images
foreach ($images as $image) {
// Construct path to thumbnail
$thumbnail = $thumbs_directory .'/'. basename($image);
// Check if thumbnail exists
if (!file_exists($thumbnail)) {
continue; // skip this image
}
// .. continue as before
echo '
<div class="pic '.$nomargin.'" style="background:url('.$thumbnail.') no-repeat 50% 50%;">
'.$title.'
</div>
';
}
}
I have some code that prints out the contents of a directory onto a webpage, what seems to be escaping me is how to make it print out alphabetically.
<?php
$dir="../zpress/pages"; // Directory where files are stored
if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
if(!is_dir($filename))
{
?>
<p><a href="../zpress/pages/<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}
?>
Any suggestion would be most welcome.
You can use scandir which will returns all files in the directory sorted alphabetically
$files = scandir($dir);
foreach($files as $file) {
// your code here
}
scandir
$the_files = array();
while(($filename = readdir($dir_list)) !== false) {
if(!is_dir($filename)) {
array_push($the_files,$filename);
}
}
sort($the_files);
foreach($the_files as $the_file) { ?>
<p><?php echo $the_file;?></p>
<?php } ?>
You can slurp the entire directory list into memory and then apply strnatcasecmp to sort the list:
$dir = ".";
$files = glob("$dir/*");
usort($files, 'strnatcasecmp');
// $files is now sorted
Using strnatcasecmp will give you the order in natural case sort, making for more human-readable output. See here for an explanation: http://sourcefrog.net/projects/natsort/
I am currently using SimpleXML to reading an xml file to generate my first array. This array displays a list of images that are found in a directory and written to xml file.
This has worked for me in the past until I upload new photos to the same album. I like to know what one does to get a complete list of images that got uploaded to the end of that first array. Any suggestions?
XML File:
<album>
<image path="/albums/1/images/100_4124.jpg" />
<image path="/albums/1/images/100_4307.jpg" />
<image path="/albums/1/images/100_4335.jpg" />
</album>
SimpleXML to get array:
$xml = simplexml_load_file('/albums/1/photos.xml');
foreach ($xml->image as $image) {
echo '<li><img src="'.$image.'"></li>';
}
What I am getting for my results:
<li><img src="/albums/1/images/100_4124.jpg"></li>
<li><img src="/albums/1/images/100_4307.jpg"></li>
<li><img src="/albums/1/images/100_4335.jpg"></li>
Scanning the directory for all images:
if ($handle = #opendir('/albums/1/')) {
$filenames = array();
while (false !== ($file = readdir($handle))) {
$ext = substr($file, strrpos($file, '.') + 1);
if ($file != '.' && $file != '..') {
$filenames[] = $file;
$total++;
}
}
}
closedir($handle);
foreach ($filenames as $filename) {
echo '<li><img src="'.$filename.'"></li>';
}
What I would like to get for results using both arrays:
<li><img src="/albums/1/images/100_4124.jpg"></li>
<li><img src="/albums/1/images/100_4307.jpg"></li>
<li><img src="/albums/1/images/100_4335.jpg"></li>
<li><img src="/albums/1/images/100_9000.jpg"></li>
<li><img src="/albums/1/images/100_9001.jpg"></li>
<li><img src="/albums/1/images/100_9002.jpg"></li>
The last 3 images that are missing from xml file would be added it to the end of the list.
Perhaps this would work for you?
<?php
$images = array();
foreach ($xml->image as $image) {
$images[] = $image;
}
foreach (array_diff($filenames, $images) as $image) {
$images[] = $image;
}
foreach ($images as $image) {
echo '<li><img src="' . $image . '" /></li>';
}
?>
This is assuming that $image from the $xml->image and $filename from your dir search are formatted the same. From what you've shown they are. If they aren't should be easy enough to run them through a quick regex to get them in a state where they are comparable.
Any who it looks like array_diff() is what you're looking for.
If you get a full list of the file names in the directory you could use PHP in_array and search for it in an array of your paths
$paths = array();
foreach ($xml->image as $image) {
$paths[] = $image['path'];
}
foreach ($filenames as $filename) {
if (!in_array($filename, $paths) {
$newImage = $xml->addChild('image');
$newImage->addAttribute('path', $filename);
}
}
// save adjusted xml file, assuming you have permission
$xml->asXML('/albums/1/photos.xml');
foreach ($xml->image as $image) {
echo '<li><img src="'.$image.'"></li>';
}