Jump to content


Photo

Snippet - List files in dir


  • Please log in to reply
7 replies to this topic

#1 Rickyx

Rickyx

    Etomite Forum Newbie

  • Member
  • 38 posts

Posted 03 May 2011 - 01:13 PM

Hi,

I'm trying to write a snippet that list files in a specific directory.

Here's the code:

/*
Snippet name: ListImgInDir
Snippet description: Create a list of images found in directory
Revision: 0.1

Description:
  List images created in a directory:
    '	"<img src='immagini/".$file."'>"	'

Snippet Author:
  Mao80 & Luca Porru
  
Snippet Category:
  Miscellaneous

Usage:
  Insert [[ListImgInDir]] anywhere in the appropriate section of your template.
*/

// *** Script ***

function elencafiles($dirname,$arrayext){
	
	$pre="<li><a href=\"../images/";
	$post="\" title=\"Progetto\">Slideshow</a></li>";
	
	$arrayfiles=Array();
	if(file_exists($dirname)){
		$handle = opendir($dirname);
		while (false !== ($file = readdir($handle))) { 
			if(is_file($dirname.$file)){
				$ext = strtolower(substr($file, strrpos($file, "."), strlen($file)-strrpos($file, ".")));
				echo $dir . $pre . $file . $post; // echo "<img src='images/".$file."'>";
				if(in_array($ext,$arrayext)){
					array_push($arrayfiles,$file);
				}
			}
		}
		$handle = closedir($handle);
	}
	sort($arrayfiles);
	return $arrayfiles;
}



$array_extimg=array('.jpg','.jpeg','.gif');
$arrayfile=array();
$arrayfile=elencafiles("images/",$array_extimg);

I can't figure what is wrong.

And I would like to call the directory from the snippet as [[ListImgInDir&dir=images]]. If i'm not wrong is easy as call the variable as is automatically added, right?


Thank you,
Rickyx

Edited by Rickyx, 03 May 2011 - 02:50 PM.


#2 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 03 May 2011 - 05:11 PM

The following modified code works on my system as a stand-alone script... It shouldn't take much to convert it into an Etomite snippet from this point... The operative variables are $array_extimg and $imgPath (new)... I don't have time at the moment to tweak this into a fully working snippet but this should get you headed in the right direction... Check out a few of the existing snippets if you need some ideas or ask questions here and we'll guide you along...

Remember, the code below is a stand-alone script so you will need to remove the PHP tags when converting into a snippet... And it might just be me but I'd do all of the HTML markup stuff outside the function call like my sample code does...

<?php
// ListImgInDir.php

/*
Snippet name: ListImgInDir
Snippet description: Create a list of images found in directory
Revision: 0.1

Description:
  List images created in a directory:
	'   "<img src='immagini/".$file."'>"    	'

Snippet Author:
  Mao80 & Luca Porru

Snippet Category:
  Miscellaneous

Usage:
  Insert [[ListImgInDir]] anywhere in the appropriate section of your template.
*/

// *** Script ***

function elencafiles($dirname,$arrayext)
{
  $pre = "<li><a href=\"{$dirname}";
  $post = "\" title=\"Progetto\">Slideshow</a></li>\n";
  $arrayfiles = array();

  if(file_exists($dirname))
  {
	$handle = opendir($dirname);
	while (false !== ($file = readdir($handle)))
	{
  	if(is_file($dirname.$file))
  	{
    	$ext = strtolower(substr($file, strrpos($file, "."), strlen($file)-strrpos($file, ".")));
    	//echo $dirname . $pre . $file . $post;
    	//echo "<img src='".$dirname.$file."'>";
    	if(in_array($ext, $arrayext))
    	{
      	array_push($arrayfiles, $file);
    	}
  	}
	}
	$handle = closedir($handle);
  }
  sort($arrayfiles);
  return $arrayfiles;
}

$array_extimg = array('.jpg','.jpeg','.gif'); // modify as required
$imgPath = "../assets/images/"; // modify as required
$arrayfile = array();
$arrayfile = elencafiles($imgPath, $array_extimg);

// *** add snippet display logic here, remembering that you cannot use echo ***

// display the array of image names (testing only)
echo "<pre>\n";
print_r($arrayfile);
echo "</pre>\n";


// display actual images (testing only)
foreach($arrayfile as $img)
{
  echo "<img src=\"{$imgPath}{$img}\" alt=\"{$img}\"><br/>\n";
}

?>


#3 Rickyx

Rickyx

    Etomite Forum Newbie

  • Member
  • 38 posts

Posted 05 May 2011 - 11:09 AM

Thank you,
is it working nicely and i'm able to create the correct list.

The problem is that it always append the result at the beginning of the document:
calling the snippet in a document like:
<ol class="slides"> [[ListImgInDir]] </ol>

I obtain:
<li><img src="./images/1HOME.jpg" alt="Progetti di Stil&ograve;" /></li>
<li><img src="./images/3HOME.jpg" alt="Progetti di Stil&ograve;" /></li>
<li><img src="./images/4HOME.jpg" alt="Progetti di Stil&ograve;" /></li>
<li><img src="./images/signora1.jpg" alt="Progetti di Stil&ograve;" /></li>
<li><img src="./images/signora2.jpg" alt="Progetti di Stil&ograve;" /></li>
<li><img src="./images/signora3.jpg" alt="Progetti di Stil&ograve;" /></li>
<ol class="slides">  </ol>

And also linking in a sub document the list is always at the top of the code. Where I'm wrong?

Thank you,
Rickyx

#4 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 05 May 2011 - 01:10 PM

You do know that you can't use echo in snippets, right...???

The way to write that snippet is to store all of the markup in a variable, like $output, and then return it at the end of the snippet... That's why I suggested studying some of the existing snippets... So, in your case, the last line of your snippet might be something like...

return "<ol class=\"slides\">\n{$output}</ol>\n";

Post up you current snippet code and I'll take a look at it...

#5 Rickyx

Rickyx

    Etomite Forum Newbie

  • Member
  • 38 posts

Posted 12 May 2011 - 08:56 PM

Thank you,

at the and I found so easy to use this snippet:
http://www.etomite.c...foldercontents/

The website is finished ;)
again thank you,
Ricky

Edited by Rickyx, 12 May 2011 - 08:56 PM.


#6 Rickyx

Rickyx

    Etomite Forum Newbie

  • Member
  • 38 posts

Posted 25 August 2011 - 05:51 PM

Well, the snippet finally done:

/*
Snippet name: listimg
Snippet description: Create a list of images found in directory
Revision: 0.2
Description:
  List images in a directory:
Snippet Author:
  Mao80 & Luca Porru & Rickyx
Snippet Category:
  Miscellaneous
Usage:
  Insert eg. [ [listimg?dirname=./templates/AlexisProRedux/images/] ] anywhere in the appropriate section of your template.
*/

$arrayext=array('.jpg','.jpeg','.gif');
$exit = "<br />";
$arrayfiles=array();

if(file_exists($dirname)){
	$handle = opendir($dirname);
	while (false !== ($file = readdir($handle))) { 
		if(is_file($dirname.$file)){
			$ext = strtolower(substr($file, strrpos($file, "."), strlen($file)-strrpos($file, ".")));
			$exit .= "<img src=\""."$dirname"."$file"."\">"."<br />";
			if(in_array($ext,$arrayext)){
				array_push($arrayfiles,$file);
			}
		}
	}
	$handle = closedir($handle);
}
sort($arrayfiles);
return $exit;


#7 Rickyx

Rickyx

    Etomite Forum Newbie

  • Member
  • 38 posts

Posted 26 July 2012 - 04:01 PM

A more complex version. Files are sorted and not listed as in filesystem and the height is normalized, in this case is 300px.

/*
Snippet name: FolderContentsForJQ
Snippet description: Create a list of images found in directory and normalize the dimensions
Revision: 0.3
Description:
  List images in a directory:
Snippet Author:
  Mao80 & Luca Porru & Rickyx
Snippet Category:
  Miscellaneous
Usage:
  Insert eg. [ [listimg?dirname=./templates/AlexisProRedux/images/] ] anywhere in the appropriate section of your template.
*/
$arrayext=array('.jpg','.jpeg','.gif');
$exit = "";
$stack = array();
if(file_exists($dirname)){
$handle = opendir($dirname);
while (false !== ($file = readdir($handle))) {
  if(is_file($dirname.$file)){
  //Minuscolizza
   $ext = strtolower(substr($file, strrpos($file, "."), strlen($file)-strrpos($file, ".")));
  //Setta le dimensioni
   $info = getimagesize("$dirname$file");
   $height = "300";
   $attr = "";
   //Normalizzo la grandezza
   $long_width =("$height"*"$info[0]"/"$info[1]");
   $width =floor($long_width);

  //Aggiunge la stringa all'array
   $list = "<a href=\""."$dirname"."$file"."\"><img src=\""."$dirname"."$file"."\" alt=\""."CUSTOMIZE"."\" "."height=\""."$height"."\" "."width=\""."$width"."\" /></a>"."\n";
   array_push($stack, "$list");
  }
}
$handle = closedir($handle);
}
sort($stack);
foreach ($stack as $key => $val) {
	$exit .= $val;
}
return $exit;

Edited by Rickyx, 26 July 2012 - 04:02 PM.


#8 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 21 September 2012 - 08:41 AM

I just saw this snippet.
In case anyone cares, I have created something similar. The below snippet returns all the pictures in a directory in reversed chronilogical order (most recent first) and allows for pagination:
//<?php

$dir = "/assets/images/dircontainingtheimagesyouwanttoshow/";
$serverdir = dirname(__FILE__) .$dir;

//$images = glob($serverdir ."*.jpg");
$images = glob( $serverdir . "{*.jpg,*.gif,*.png}", GLOB_BRACE); //add other extensions if needed
array_multisort(array_map('filemtime', $images), SORT_DESC, $images);

$page= 1;
if(isset($_GET['page'])) { $page=$_GET['page']; }

$nrpages = ceil(count($images)/10);
$foot = "<div style=\"text-align:center; font-size:12px; margin:10px 0;\">";

if ($nrpages < 6) {
$i = 1;
while ($i <= $nrpages){
if ($i == $page) { $foot .= " | " . $i; }
else $foot .= " | <a href=\"?page=". $i ."\">". $i ."</a>";
$i++;
}
}
else {
if (1 == $page) { $foot .= " 1 "; }
else $foot .= "<a href=\"?page=1\">1</a>";
if ($page > 4){
$foot .= " &nbsp; ... &nbsp; ";
}
$i = $page-2;
for ($j=0;$j<5;$j++){
if ($i>1 && $i < $nrpages){
if ($i == $page) { $foot .= " | " . $i; }
else $foot .= " | <a href=\"?page=". $i ."\">". $i ."</a>";
}
$i++;

}
if ($i < $nrpages) {
$foot .= " &nbsp; ... &nbsp; ";
}
if ($nrpages == $page) { $foot .= " | " . $nrpages ; }
else $foot .= " | <a href=\"?page=". $nrpages ."\">". $nrpages ."</a>";

}
$foot .= "</div>";

//return $images[0]; //returns only most recent

//return all images
for($i=0;$i<10;$i++) {
$returnblock .= "<p align=\"center\"><img src=\"". $dir . substr($images[(($page-1)*10 + $i)], strlen($serverdir))."\" /></p>";
}

return $returnblock . $foot ;


Edited by vampke, 21 September 2012 - 08:42 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users