Insert custom CSS class into function? - php

I want to use my custom CSS style on that code, any ideas how it should be done?
//Load modules
if ($handle = opendir('modules'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$replace = preg_replace( array ('/(.*?)/', '/[-]/', '/.php/'), array ('$1', ' '), $file);
$result = ucfirst($replace);
echo '<a href="?inav=';
echo $replace;
echo '">';
echo $result;
echo '</a><br>';
}
}
closedir($handle);
And that I want to insert:
<a href="?inav=" class="button101"><span class="user icon">

Just add the echo you need:
//Load modules
if ($handle = opendir('modules'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$replace = preg_replace( array ('/(.*?)/', '/[-]/', '/.php/'), array ('$1', ' '), $file);
$result = ucfirst($replace);
echo '<a href="?inav=';
echo $replace;
echo '"';
echo ' class="button101"';
echo '>';
echo $result;
echo '</a><br>';
}
}
closedir($handle);

Related

How do I sort pictures alphabetically in gallery?

I have this php gallery and I need to sort pictures alphabetically. How can I do that? Thank you.
<?php
$dir = htmlspecialchars($_GET["dir"]);
$className = htmlspecialchars($_GET["className"]);
if ($handle = opendir($dir)) {
echo "<div class='photogallery $className'><ul>";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
$path = $dir."/".$entry;
$pathThumb = $dir."/thumbs/".$entry;
echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
</li>";
}
}
closedir($handle);
echo "</ul><div class='both'></div></div>";
}
?>
glob returns the directory sorted unless you specify otherwise.
echo "<div class='photogallery $className'><ul>";
foreach (glob($dir . "/*.*") as $entry) {
if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
$path = $dir . "/" . $entry;
$pathThumb = $dir . "/thumbs/" . $entry;
echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
</li>";
}
}
echo "</ul><div class='both'></div></div>";

Search for a folder and and get the content of the files inside

I'm trying to search for a folder and retrieve the files inside of the folder (get content) I'm able to search for the folder using the follow code but I can't pass from there I can't see the content an retrieve the files inside. The files inside will be txt files and I would like to be able to open and see then.
How can achieve what i want? Thank you.
<?php
$dirname = "C:\windows";//Directory to search in. *Must have a trailing slash*
$findme = $_POST["search"];
$dir = opendir($dirname);
while(false != ($file = readdir($dir))){//Loop for every item in the directory.
if(($file != ".") and ($file != "..") and ($file != ".DS_Store") and ($file !=
"search.php"))//Exclude these files from the search
{
$pos = stripos($file, $findme);
if ($pos !== false){
$thereisafile = true;//Tell the script something was found.
echo'' . $file . '<br>';
}else{
}
}
}
if (!isset($thereisafile)){
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
?>
New code
<?php
$dirname = "C:\\Windows\\";//Directory to search in. *Must have a trailing slash*
$findme = 'maxlink'; //$_POST["search"];
$files = scandir($dirname);
foreach ($files AS $file)
{
if ($file == '.' or $file == '..' or $file == '.DS_Store' or $file == 'search.php') continue;
if (stripos($file, $findme) !== false)
{
$found = true;
echo 'FOUND FILE ' . $file . '<hr>';
echo 'OPENING IT:<br>';
echo file_get_contents($dirname . $file);
echo '<hr>';
}
else
{
echo 'not found: ' . $file . '<br>';
}
}
if (!isset($found))
{
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
The following code uses a recursive function for searching the directory. I hope it’ll solve your problem.
function scandir_r($dir){
$files = array_diff(scandir($dir), array(".", ".."));
$arr = array();
foreach($files as $file){
$arr[] = $dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
$arr = array_merge($arr, scandir_r($dir.DIRECTORY_SEPARATOR.$file));
}
}
return($arr);
}
$dirname = "C:\windows";
$findme = "/".preg_quote($_POST["search"], "/")."/";
$files = preg_grep($findme, scandir_r($dirname));
if(sizeof($files)){
foreach($files as $file){
$_file = $dirname.DIRECTORY_SEPARATOR.$file;
echo "$file<br/>";
}
}
else{
echo "Nothing was found.";
echo "<img src=\"yourimagehere.jpg\"/>";
}

I want to show images in the directory online but it not show the images properly how I can do this

I am trying to show images from online directory but images not shown properly and breaks what I can do to show these all images properly
<?php
// Connects to your Database
include_once("connection.php");
//read image directory
$images_dir = '/home/qeplaho/public_html/image/';
function ListFiles($images_dir) {
if($dh = opendir($images_dir)) {
$files = Array();
$inner_files = Array();
while($file = readdir($dh)) {
if($file != "." && $file != ".." && $file[0] != '.') {
if(is_dir($images_dir . "/" . $file)) {
$inner_files = ListFiles($images_dir . "/" . $file);
if(is_array($inner_files)) $files = array_merge($files, $inner_files);
} else {
array_push($files, $images_dir . "/" . $file);
}
}
}
closedir($dh);`enter code here`
return $files;
}
}
echo "<table>";
foreach (ListFiles('/home/qeplaho/www/image/') as $key=>$file){
echo "<tr>";
echo"<td>";
echo '<img src=\"$images_dir\" width="200" height="200"/>';
echo "</td>";
echo "</tr>";
}
echo "</table>";`<code>
this should do the trick:
$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
echo '<img src="pictures/'.$file.'" border="0" />';

Strip the extension from a file

I have a code that is working as i want it to apart from 1 problem.
You can see the result here http://test.whatanswered.com/health/what-can-a-first-aider-do.php on the right below "Related Articles" showing dead links.
The following is the HTML that should be displayed.
<p>Name of the page</p>
I would like to strip the dashes and php as above "Name of the page" but it also strips the dashes and php from the url.
The code is:
<?php
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[$file] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' ');
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file => $health) {
$thelist .= '<p>'.$health.'</p>';
}
}
?>
<?=$thelist?>
I've refactored your code a little - the $fileTab array now just stores filenames and converting this to a title happens at the point of display:
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$thelist .= '<p>'.$title.'</p>';
}
}

PHP script to parse directory, list all images and add class="last" to the last image in the directory

I'm able to parse through the directory and list all images with any of the functions below. I just need to insert a class="last" attribute into the img tag of the last element in the loop.
Also, which of these functions works best for what I'm trying to do?
Any help much appreciated!
function get_images1() {
$exts = 'jpg jpeg png gif';
$str = ''; $i = -1; // Initialize some variables
$folder = './wp-content/uploads';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
//$str .= $file;
$str .="<img src='wp-content/uploads/". $file ."' alt='" . $file . "' />";
//if ($str) $str .= '|';
++$i;
}
}
}
echo $str;
closedir($handle); // Were not using it anymore
return $str;
}
function get_images2() {
//Open images directory
$dir = # opendir("wp-content/uploads/");
//List files in uploads directory
while (($file = readdir($dir)) !== false)
{
if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file))
{
echo '<img src="wp-content/uploads/'. $file .'" alt="" />';
}
}
closedir($dir);
}
function get_images3() {
$dir = 'wp-content/uploads/';
$files = scandir($dir);
//print_r($files);
$num = count($files);
for($n=0; $n<$num; $n++)
{
if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $files[$n]))
{
echo '<img src="wp-content/uploads/'. $files[$n] .'" alt="" />';
}
}
}
function get_images()
{
$directory = 'wp-content/uploads/';
$directory_stream = # opendir($directory);
// Display information about the directory stream
// print_r ($directory_stream);
while ($entry = readdir ($directory_stream))
{
if (! is_file ("$directory/$entry"))
continue;
echo '<img src="wp-content/uploads/'. $entry .'" alt="" />';
}
}
If you don't echo or concatenate the <img> and add the filename to an array you can easily generate the markup you want.
<?php
$dir = 'wp-content/uploads/';
$imgs = array();
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
array_push($imgs, $file);
}
}
closedir($dh);
} else {
die('cannot open ' . $dir);
}
foreach ($imgs as $idx=>$img) {
$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
echo '<img src="' . $dir . $img . '" alt="' .
$img . '"' . $class . ' />' . "\n";
}
?>
I would use DirectoryIterator to get all files, the filter this using a custom FilterIterator and then a CachingIterator to check for the last element:
class ExtensionFilter extends FilterIterator {
public function accept() {
return $this->current()->isFile() && preg_match("/\.(bmp|jpe?g|gif|png)$/", $this->current()->getBasename());
}
}
$it = new CachingIterator(new ExtensionFilter(new DirectoryIterator($dir)));
foreach ($it as $dir) {
$filename = $dir->getFilename();
echo '<img src="'.$filename.'" '.($it->hasMore() ? '' : ' class="last"').'>';
}

Categories