I have a folder / directory containing lots of images and other folders / directories.
I am showing the preview of these files using following code:
<?php
$images=array();
$dir_handler = opendir('test') or die("Unable to open path");
$i=0;
while($file = readdir($dir_handler))
{
if(is_dir($file))
continue;
else if($file != '.' && $file != '..' && $file != 'index.php')
{
$images[$i]=$file;
$i++;
}
}
sort($images);
for($i=0; $i<sizeof($images); $i++)
{
echo "<img style='border:1px solid #666666; width:100px;height:100px; margin: 10px;' src='test/".$images[$i]."'/><input type='button' value='nok[]'>";
} closedir($dir);
?>
The problem is that I want to assign a separate button to each file (to each image or folder), So that by clicking each button, its corresponding image (or folder / directory) gets deleted from the main folder and no more being shown as a preview.
Another small problem is that the preview of folders are not being shown with the above code. Why? Any help will be appreciated.
There are several questions here.
First question: Why won't preview of folders show? This is because you show previews by looping through the $images array, but you do not add directories to that array (see your code where you check if it is_dir and then invoke "continue;"). If you want to include the directories, then you should include them in the $images array (or do something else with them).
Second question: How to do the deleting? You need to either extend your existing PHP script or write another one. You would create a link on the delete icon; the link's href would be to the new (or existing) PHP script and you would pass in as parameters the folder or file to be deleted. If it is a folder, then use rmdir(). If it is a file, then use unlink(). I can help you more with this later, if you need it.
You are always displaying an image, you need to do a check if the $images array value is an image or a folder. Then do something different with the display.
To delete a file or folder, use these functions respectively:
unlink();
rmdir();
http://www.php.net/manual/en/function.rmdir.php
http://php.net/manual/en/function.unlink.php
To add a list of folders before your images change your code to:
<?php
$images=array();
$folders = array();
$dir_handler = opendir('test') or die("Unable to open path");
$i=0;
while($file = readdir($dir_handler))
{
if(is_dir($file)) {
$folders[count($folders)] = $file;
}
else if($file != '.' && $file != '..' && $file != 'index.php')
{
$images[$i]=$file;
$i++;
}
}
sort($images);
foreach($folders as $folder){
echo "<a href='#'>$folder</a>";
}
for($i=0; $i<sizeof($images); $i++)
{
echo "<img style='border:1px solid #666666; width:100px;height:100px; margin: 10px;' src='test/".$images[$i]."'/><input type='button' value='nok[]'>";
}
closedir($dir);
?>
Then all you have to do is build out the visual elements of a folder in your view and link to a function that will do your unlink() and rmdir() where needed.
Related
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);
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"
I have the following PHP code to display the file structure of my site. This gives my a very nice indented structure with folder and file images.
How can I add JQuery to this so I can collapse and expand the folders? I tried a couple of jquery plugins but they didn't work.
Can you suggest a jquery plugin or an article or code snippet?
Thank you!
<?php
$path = ROOT_PATH;
$dir_handle = #opendir($path) or die("Unable to open $path");
list_dir($dir_handle,$path);
function list_dir($dir_handle,$path)
{
echo "<ul>";
while (false !== ($file = readdir($dir_handle)))
{
$dir =$path.'/'.$file;
if(is_dir($dir) && $file != '.' && $file !='..' )
{
$handle = #opendir($dir) or die("undable to open file $file");
echo '<li><input name="" type="image" src="themes/default/images/explore/folder.png" />'.$file.'</li>';
list_dir($handle, $dir);
}
elseif($file != '.' && $file !='..')
{
echo '<li><input name="" type="image" src="themes/default/images/explore/file.png" />'.$file.'</li>';
}
}
echo "</ul>";
closedir($dir_handle);
}
?>
With the structure you have, a nested <ul> tree, this should be relatively easy.
Your folders all have <li> elements with anchors in them, not quite sure why as you have input-images inside them which are clickable in their own right. Put a class on these anchors or the input called "folder" or somesuch. Then all you need to do is hide all the ULs to start with apart from the root folder and use jQuery to show/hide the nested <ul> lists when a user clicks on the associated folder.
Providing you setup that "folder" class, try the following code.
/* if you want to hide all but the root with code */
$('ul:gt(0)').hide();
$('.folder').click(function() {
$(this).parents('li:first').next('ul').slideToggle('slow');
return false;
});
I have created this php script which displays the contents of a designated directory and allows users to download each file. Here is the code:
<?php
if ($handle = opendir('test')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href='test/$file'>$file\n</a><br/>";
}
}
closedir($handle);
}
?>
This script also displays folders, but when I click a folder, it does display the contents of the folder, but in the default Apache autoindex view.
What I would like the script to do when a folder is clicked, is display the contents, but in the same fashion as the original script does (as this is more editable with css and the like).
Would you know how to achieve this?
Don't create a link to the directory itself, but to a php page which displays the contents.
Change your php code to somthing like:
if(isset($_REQUEST['dir'])) {
$current_dir = $_REQUEST['dir'];
} else {
$current_dir = 'test';
}
if ($handle = opendir($current_dir)) {
while (false !== ($file_or_dir = readdir($handle))) {
if(in_array($file_or_dir, array('.', '..'))) continue;
$path = $current_dir.'/'.$file_or_dir;
if(is_file($path)) {
echo ''.$file_or_dir."\n<br/>";
} else {
echo ''.$file_or_dir."\n<br/>";
}
}
closedir($handle);
}
PS write you html code with double quotes.
You need your HREF to point back to your PHP script, and not the directory. You will then need to update your PHP script to now which directory it needs to read.
I tried to write a script to list all files in directories and subdirectories and so on.. The script works fine if I don't include the check to see whether any of the files are directories. The code doesn't generate errors but it generates a hundred lines of text saying "Directory Listing of ." instead of what I was expecting. Any idea why this isn't working?
<?php
//define the path as relative
$path = "./";
function listagain($pth)
{
//using the opendir function
$dir_handle = #opendir($pth) or die("Unable to open $pth");
echo "Directory Listing of $pth<br/>";
//running the while loop
while ($file = readdir($dir_handle))
{
//check whether file is directory
if(is_dir($file))
{
//if it is, generate it's list of files
listagain($file);
}
else
{
if($file!="." && $file!="..")
echo "<a href='$file'>$file</a><br/>";
}
}
//closing the directory
closedir($dir_handle);
}
listagain($path)
?>
The first enties . and .. refer to the current and parent directory respectivly. So you get a infinite recursion.
You should first check for that before checking the file type:
if ($file!="." && $file!="..") {
if (is_dir($file)) {
listagain($file);
} else {
echo ''.htmlspecialchars($file).'<br/>';
}
}
The problem is, variable $file contains only basename of path. So, you need to use $pth.$file.