PHP to build gallery navigation - php

I was wondering if it is possible to use my server's file structure to automatically build a navigation menu for an image gallery.
Currently I have a simple "showcase" with hard-coded links to different folders of images (using jquery and ajax and php, some things that I don't quite understand but learned how to use from tutorials and the like). Basically, I have three files:
main.php
main.css
images.php
and I use hard links on main.php to call the images.php script to load a specific folder containing images into a div on the main page.
Here is my current nav setup:
<ul>
<li>Animals</li>
<li>People</li>
<li>Objects</li>
</ul>
My question is: Since all my images are in subdirs under the "images" dir, is there a way I can just build the navigation points (php script?) using the names of the subdirs in "images"? (such that it is kept up to date when I add more folders)
also, for some reason I can't make the variables on my script include the 'images.php?dirname=images/', is there any way to fix that?

If they are all in the images directory, you can specify a $image_path
<?php
$image_path = '/full/path/to/images';
if ($_GET['gallery']) {
$gallery_path = $image_path . '/' . $_GET['gallery'];
# if $_GET['gallery'] is `animals`:
#
# $gallery_path = '/full/path/to/images/animals'
# load your images within this path
}
?>
And to get all the subdirectories use dir
<?php
$image_path = '/full/path/to/images';
$d = dir($image_path);
while (false !== ($entry = $d->read())) {
if (is_dir($image_path . $entry)) {
if (($entry != '.') || ($entry != '..')) {
echo $entry; # or print your html code for each directory.
}
}
}
?>

Related

List files in same directory using PHP

I'm trying to list files in a folder. I have done this before, so I am not sure why I am having a problem now.
I have a PDF files I am trying to display to my web page. The directory structure looks like this:
folder1/folder2/displayFiles.php
folder1/folder2/files.pdf
displayFiles.php is the process file where I am using the code below.
I am trying to display the file called files.pdf onto the page, which is in the same directory as the process file.
Here is my code so far:
<?php
$dir = "folder1/folder2/";
// $dir = "/"; <-- I also tried this
$ffs = scandir($dir);
foreach($ffs as $ff)
{
if($ff != '.' && $ff != '..')
{
$filesize = filesize($dir . '/' . $ff);
echo "<ul><li><a download href='$dir/$ff'>$ff</a></li></ul>";
}
}
?>
I know it's a simple fix. I just cannot find the code to fix it.
Your $dir is pointing at a non-existent folder
Change the dir to point to the folder correctly $dir = ".";.
Just use glob
http://php.net/manual/de/function.glob.php
$pdfs = glob("*.pdf"); // if needed loop through your directorys and glob files
print_r($pdfs);
Just an example. You should be able to use it with some edits.

PHP delete (.extension) files that are modified after specific time from directory and all sub-directories

I need a little bit of help. I need to write a script that will look through all directories and sub-directories and delete specific extensions that are modified after specific time. I can get it to delete files from specific path, but I need the script to search inside sub directories. Is there any way to do that?
I have tried this for files but I have no idea on how to make it work with directories and sub directories
$path = '/path/to/file/';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($path . $file);
if( $filelastmodified > "MY TIME STAMP" )
{
unlink($path . $file);
}
echo "deleted";
}
closedir($handle);
}
Create a recursive function, if it finds a folder it calls itself passing the folder name.

PHP - Scan dir for folders and txt

I am dynamically building an accordion menu. Accordion will get the header information from folder names and contents from .txt files associated to folder names. They are relatives in terms of directory.
<div class="accordion">
<?php if($_GET['cat']!='') {
$handleCat = 'tv/'.$_GET['cat'];
$category = scandir($handleCat);
$i = 1;
foreach ($category as &$value) {if ((!in_array($value,array(".","..","...")))){
echo '<div class="header">'.$value.'</div><div class="content" id="ac'.$i.'">'.file_get_contents($value.".txt", false).'</div>';
$i+=1;}}}
?>
</div>
In my code there are two problems. First one is logic problem. I couldn't made up scan foldernames and file names seperately. Forexample program1.txt also becomes a headername. Second problem is method problem. I found file_get_contents() method but this doesn't extracts .txt file contents.
You can distinguish files from folders using the function is_dir().
As of file_get_contents, it reads the file contents but does not echo it. Use :
echo '<div class="header">'.$value.'</div>'.$value.'<div class="content" id="ac'.$i.'">';
echo file_get_contents($value.".txt", false);
echo'</div>';
Use the following to list files in a directory. Where I commented code you can do whatever you want with that particular file. You can use is_dir() to distinguish from files and directories and then proceed accordingly.
<?php
if ($dir = opendir('.')) {
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
echo "$file\n";
//code
}
}
closedir($handle);
}
?>
Read the contents of a file using the following code.
$contents = file_get_contents($file);

Display a random link from my site?

I have a website with a lot of static content pages which are divided by categories, each category has a folder and index.html inside
What I want to do is to put some code inside the main index.php file in a way that whenever a user visits this index.php file, he will be redirected to one of my folders,
For example:
index.php code :
<?php
random_redirect_the_user();
?>
Where random_redirect_the_user() will redirect the user to http://www.example.com/wiki/(FOLDER)
And (FOLDER) is randomly chosen and can be any folder of the ones that appear above.
The question :
What shall I write inside the random_redirect_the_user() to perform this kind of redirection ?
Try something like this: First, get the array of names of all folders:
$dir = '/tmp'; //here set you main folder
$foldersArray = scandir($dir)
Then, get a random name:
$randomFolderKey = array_rand($foldersArray);
After that, do the following:
header('Location: www.youwebsiteUrl/'.$foldersArray[$randomFolderKey])
var $directories = Read your folder list from the server (Search for: PHP Read directories)
Get a random directory (Search for: PHP random)
// You can get a random between 0 and count($directories)
Then redirect your user: (Search for: PHP redirect | PHP header )
And you are done!
This should do what you are looking for.
Hope it helps
function random_redirect_the_user(){
$dir_list = array();
$path = '/path/to/directory/';
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if($entry == '.' || $entry == '..'){
continue;
}
if(is_dir ( $path.''.$entry )){
array_push($dir_list, $path.''.$entry);
}
}
}
$count = count($dir_list);
$random = rand(0,$count);
return $dir_list[$random];
}
$dir = random_redirect_the_user();
echo $dir;

PHP to post links to sub directories & php to display images

I'm very basic when it comes to PHP.
With my website, I have a directory called "uploads"
Within "uploads" I have 3 folders "Example1" "Example2" and "Example3"
Within each of the folders, they contain images.
I need to know how to use php to create a navigation for every sub directory.
So that if I add a new folder "Example4" it will give a navigation like:
Select what section you're looking for:
Example1 | Example2 | Example3
and if I later add new folders add them to the navigation.
EX:
Example1 | Example2 | Example3 | Example4 | Example5
Then once they click the link to go into the folder, have a code that displays all the images in that folder.
So far I have:
<?php
$files = glob("uploads/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="/'.$num.'">'."<p>";
}
?>
but it will only display the images in the upload directory, not the images in Example1 and so on.
How on earth would I go about doing this? I'm doing it for a school project and have two weeks to complete it, but I am so lost. I only have knowledge with CSS, HTML, and the only PHP I know is php includes, so any help would be appreciated.
Since it seems that you are familiar with globs a bit, here is an example using the "glob" function. You can see a basic working example of what you are looking for here:
http://newwebinnovations.com/glob-images/
Here is how I have the example set up:
There are two PHP files, one is index.php and the other is list-images.php.
There is also a folder for images two subfolders that have images inside of them.
index.php is the file that finds the folders in the images folder and places them in a list with links list-images.php which will display the images inside of the folder:
$path = 'images';
$folders = glob($path.'/*');
echo '<ul>';
foreach ($folders as $folder) {
echo '<li>'.$folder.'</li>';
}
echo '</ul>';
The links created above have a dynamic variable created that will pass in the link to the list-images.php page.
Here is the list-images.php code:
if (isset($_GET['folder'])) {
$folder = $_GET['folder'];
}
$singleImages = array();
foreach (glob($folder . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $image) {
$imageElements = array();
$imageElements['source'] = $image;
$singleImages[$image] = $imageElements;
}
echo '<ul>';
foreach ($singleImages as $image) {
echo '<li><img src="'.$image['source'].'" width="400" height="auto"></li>';
}
echo '</ul>';
The links created here will link you to the individual images.
To get files of every specific folder ,pass it throw a get variable that contains folder's name,an then scan this folder an show images ,url should be like this :
listImages.php?folderName=example1
To have menu like what you want :
<?php
$path = 'uploads/' ;
$results = scandir($path);
for ($i=0;$i<count($results);$i++ ) {
$result=$results[$i];
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
echo "<a href='imgs.php?folderName=$result'>$result</a> ";
}
if($i!=count($results)-1) echo '|'; //to avoid showing | in the last element
}
?>
And here is PHP page listImages that scan images of a specific folder :
<?php
if (isset($_GET['folderName'])) $folder=$_GET['folderName'];
$path = 'uploads/'.$folder.'/' ;
$images = glob($path . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($images as $image) {
echo "<img src='$image' />";
}
?>
First of all, do read more PHP manual, for directory related: opendir, for files related: fopen
The following code is basically re-arranging the example code provided in opendir. What it does:
A scan_directory function to simply check if directory path is valid and is a directory, then proceed to do a recursive call if there's a child directory else just print out the file name.
The first if/else condition is just to ensure the base directory is valid.
I'll added ul and li to make it slightly more presentable.
$base_dir = 'upload';
if (is_dir($base_dir))
scan_directory($base_dir);
else
echo 'Invalid base directory. Please check your setting.';
// recursive function to check all dir
function scan_directory($path) {
if (is_dir($path)) {
if ($dir_handle = opendir($path)) {
echo '<ul>';
while (($file = readdir($dir_handle)) !== false) {
if ($file != '.' && $file != '..') {
if (is_dir($path . '/' . $file)) {
echo '<li>';
echo $file;
scan_directory($path . '/' . $file);
echo '</li>';
}
else
echo "<li>{$file}</li>";
}
}
echo '</ul>';
}
}
}
create image as subdirectory name with image name and save it in database
example:
subdirectory name: example2
image name: image.jpg
store image name in db as "example2/image.jpg"

Categories