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;
});
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 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.
I have written this simple script display all the files in a directory as a set of buttons.
This code reads from the upload directory and displays all files inside a submit button in a form.
$handle = opendir("upload");
echo '<form name="form" method="post" action="download.php">';
while($name = readdir($handle)) {
echo '<input type="submit" name="file" value='.$name.' />';
}
echo '</form>';
Now the issue here is; every time I run the script I find two button at the beginning with contents . and ..
I have not been able to figure out what causes this issue.
What you have encountered are two special files used by the file system.
. represents the current directory you are in.1
.. represents the parent directory of the current directory.2
Footnotes:
1. A path such as "/my_dir/././././././file" is equivalent to "/my_dir/file".
2. A path such as "/my_dir/../my_dir/../my_dir/file" is equivalent to "/my_dir/file" since .. will make you move "up" one level.
To get around the issue of showing these two to your user filter the content returned by readdir using something as the below:
while ($name = readdir ($handle)) {
if ($name == '.' || $name == '..')
continue; /* don't echo anything, skip to next read */
echo '<input type="submit" name="file" value='.$name.' />';
}
the directory listing includes . for the current dir and .. for the parent dir.
I usually use this that i got from the PHP manual (http://php.net/manual/en/function.readdir.php)
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
so what you need to do is to exclude the . and .. from the output.
Another solution is to use a FilesystemIterator like this:
foreach(new FilesystemIterator('upload') as $file){
echo $file;
}
It will automatically skip . and .. entries in the filesystem.
I need to create a script that will find all my .htaccess files on my server and replace their content with the new content I need in order for my pages to be SEO friendly.
So far I've come across a few scripts that will find all the .htaccess files but I need to be able to open, replace all content with new and save with the proper permissions.
Can anyone help me with the following code to add the extra functionality I need?
<?php
function searchDir($dir) {
$dhandle = opendir($dir);
if ($dhandle) {
// loop through it
while (false !== ($fname = readdir($dhandle))) {
// if the element is a directory, and does not start with a '.' or '..'
// we call searchDir function recursively passing this element as a parameter
if (is_dir( "{$dir}/{$fname}" )) {
if (($fname != '.') && ($fname != '..')) {
echo "Searching Files in the Directory: {$dir}/{$fname} <br />";
searchDir("$dir/$fname");
}
// if the element is an .htaccess file then replace content
} else {
if($fname == ".htaccess")
{
echo "Replacing content of file ".$dir/$fname."<br />";
// I need the code for editing the files here.
}
}
}
closedir($dhandle);
}
}
searchDir(".");
?>
Modify your else loop with this
else {
if($fname == ".htaccess")
{
echo "Replacing content of file ".$dir/$fname."<br />";
// I need the code for editing the files here.
$htaccess_content = " Your htaccess string " ; // you can do this assignment at the top
file_put_contents("{$dir}/{$fname}",$htaccess_content) ;
}
}
Use file_get_contents() and file_put_contents().
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.file-put-contents.php
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.