How to display all files and folders available in current directory php (FTP)
See Attached image example....
Tree View Image
define('SITE_URL',"http://yourdomain.com");
function listFolderFiles($dir){
$fileFolderList = scandir($dir);
echo '<ul>';
foreach($fileFolderList as $fileFolder){
if($fileFolder != '.' && $fileFolder != '..'){
if(!is_dir($dir.'/'.$fileFolder)){
echo '<li><a target="_blank" href="'.SITE_URL.'/'.ltrim($dir.'/'.$fileFolder,'./').'">'.$fileFolder.'</a>';
} else {
echo '<li>'.$fileFolder;
}
if(is_dir($dir.'/'.$fileFolder)) listFolderFiles($dir.'/'.$fileFolder);
echo '</li>';
}
}
echo '</ul>';
}
listFolderFiles('uploads/'); // function call with directory path (e.g. : upload/)
Related
I got a script that gets images from a folder and puts them inside a lightgallery. All is working fine, except when I add more than 7 images (or any new images for that matter). There are 7 images in the folder when I add 8.jpg it shows nothing, not in the gallery and not in the source code. What could be the cause of this?
My script:
<div id="lightgallery">
<a href="<? echo $imagesmall; ?>">
<img class="fullnieuwsimg" src="<? echo $imagesmall; ?>">
</a>
<?
function scan_dir($dir) {
$ignored = array('.', '..', '.svn', '.htaccess','index.html');
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, $ignored)) continue;
$files[$file] = filemtime($dir . '/' . $file);
}
ksort($files,SORT_NATURAL);
$files = array_keys($files);
return ($files) ? $files : false;
}
if(is_dir($_SERVER['DOCUMENT_ROOT'].'/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/') != FALSE){
foreach(scan_dir($_SERVER['DOCUMENT_ROOT'].'/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/') as $entry) {
$gallery .= '
<a href="/_intern/SNM/cms/images/Projecten/'.$contentcr[0]['alias'].'/'.$entry.'">
<img class="galleryimgs" title="'.$contentcr[0]['alias'].'" alt="'.$contentcr[0]['alias'].'" src="/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/'.$entry.'" />
</a>';
}
}else{
echo '';
}
echo $gallery;
?>
</div>
The pictures are in the folder like this (barebone joomla cms) :
All work, except 8.jpg or any other new images I add.
I have to do a php website for college.
I have to create a password and a submit button. After the I have posted the password I should get text from a html file. What is the best to achieve that?
I tried:
foreach($files as $file2) {
if($_POST['submitPassword']){
if($file2 === '.' OR $file2 === '..' OR $file2 === 'thumbs.db' OR !is_dir($folder.'/'.$file2)) {continue;}
if(file_exists($folder.'/'.$file2.'/doubleindemnity.gif') AND file_exists($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm')) {
echo '<div class="Container">';
echo "<div class='image2'><img src='$folder/$file/doubleindemnity.gif'>";
$lines4 = file($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
$count = count($lines4);
for($a = 0;$a < $count;$a++) {
echo substr($lines4[$a],strlen($folder),strpos($lines4[$a], '.')-strlen($folder));
}
echo "</div>";
}
echo "</div>";
}
}
?>
Help would be highly appreciated:)
Cheers:)
You can easily include the contents of an html file through include(). For example:
include($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
It will call the entire file, and output it (since it's not a PHP file for execution)
This is a follow up question to a thread I came across here.
Users can upload pictures to my site, and they are saved in an 'uploads' folder. Another page on the site has a photo gallery that pulls pictures from an 'approved' folder. How do I go about including a link (or checkbox) to rename or delete each file, after the file name? i.e.:
<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li class="title">';
if(is_dir($dir.'/'.$ff)){
echo $ff;
listFolderFiles($dir.'/'.$ff);
}else{
echo ''.$ff.''.
'...Keep -
Delete';
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('uploaded_files');
?>
<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li class="title">';
if(is_dir($dir.'/'.$ff)){
echo $ff;
listFolderFiles($dir.'/'.$ff);
}else{
echo ''.$ff.'';
echo ''.$ff.'';
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('uploaded_files');
?>
In delete.php, take the File Argument $_GET['file'];
<?php
$file = "root/".$_GET['file'];
unlink($file);
location('back to the page');
?>
In rename.php get the new name and
<?php
$file = "root/".$_GET['file'];
rename($file,"New file name");
location('back to the page');
?>
Now there are two ways to going about, Use ajax in main page or in rename.php take the new file name as input and do renaming.
I would like PHP to check the directory of selection i input in variable and out put the Folder, Files, Permission (writable or not writable) at same time.
Here is my PHP Code:
<?php
$directorySelection = '../app/';
if (file_exists($directorySelection)) {
if ($existence = opendir('../app')) {
while (false !== ($files = readdir($existence))) {
if ($files != "." && $files != ".." && $files != ".DS_Store") {
echo '<table>';
echo '<tr>';
echo '<td style="width: 90%; padding: 10px;">'. $files .'</td>';
if (is_writable($files)) {
echo '<td><span class="label label-success">Writable</span></td>';
} else {
echo '<td><span class="label label-danger">Not writable</span></td>';
}
echo '<td>'. substr(sprintf('%o', fileperms($files)), -4) . '</td>';
echo '</tr>';
echo '</table>';
}
}
closedir($existence);
}
} else {
echo '<div class="alert alert-warning" role="alert">Application Directory doesn\'t exist <a role ="button" data-toggle="alertInfo" placement="left" title="Application Directory" data-content="Please set your Application Directory, so that the installer can check for folder, files and there Permissions "> <span class="glyphicon glyphicon-info-sign floatRight"></span></a></div>';
}
?>
Result i get:
the problem is with your $files variable, as it doesn't contain the full path, only the name, and it is checking from CWD.
Your code is testing for the file perms based on the CWD, but your files are not located in the CWD. so prefix the $files with the name of the directory such as
if ($files != "." && $files != ".." && $files != ".DS_Store") {
$filepath = $directorySelection . $files;
....
if (is_writable($filepath)) {
.....
substr(sprintf('%o', fileperms($filepath)), -4)
And it should read the file correctly. Then use $files if you want the name, and $filepath for function that take a filename.
(interior code not shown as to not clutter it up)
With another coder's help, I have PHP code that creates a photo gallery with auto-created thumbnail images of any image files in the directory. (It's open source, so anyone else is free to use and modify as they desire.)
I use it as a stock photo library and the file names include the keywords for that image. For example, one might be businessman-tie-suit-briefcase-office-meeting.jpg.
I've been trying to add a keyword search input that looks at the file names, but cannot figure out how to proceed. I've built keyword searches for a database, but this directory file name search is new to me.
Here's the relevant code of the page:
<?
$gallery = $_GET["gallery"];
$dir = $dir.$gallery."/";
//Put files into an array
// create a handler to the directory
$dirhandler = opendir($dir);
// read all the files from directory
$nofiles=0;
while ($file = readdir($dirhandler)) {
// if $file isn't this directory or its parent
//add to the $files array
if ($file != '.' && $file != '..')
{
$nofiles++;
$files[$nofiles]=$file;
}
}
//close the handler
closedir($dirhandler);
//Back button to appear at the top of each page to go to the previous directory if not on the main page
if ($gallery !="" or $keyword !="")
{
echo "<div><a href='javascript:history.go(-1)'><img src='images/up.png' border='0'></a></div>";
}
// BEGINNING ADD FOR KEYWORD SEARCH
// KEYWORD SEARCH BOX- create text box to search entire directory for that keyword and display page in grid pattern with results
?>
<div style='position:relative; left:10px;'>
<form action='index_search.php?keyword=$keyword' method='get' name='search'>
<input type='text' name='keyword' size='25'>
<input type='submit' value='Search Keyword'>
</form>
</div>
<?
//*************************************************************************//
// PERFORM KEYWORD SEARCH
if ($keyword !="")
{
echo "<div class='keytext'>Keyword search: <b>" . $keyword . "</b><br/></div>";
/*********************************************************************************************************/
/* ***** THIS IS WHERE THE SEARCH OF DIRECTORY FILES NEEDS TO OCCUR AND OUTPUT RESULTS AS $files */
/* get results where $file LIKE %$keyword%; */
/*********************************************************************************************************/
//Show images
foreach ($files as $file)
{
if ($file!="."&&$file!="..")
{
$extention = explode('.', $file);
if ($extention[1] != "")
{
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
echo"</a><br/>";
$file_name = current(explode('.', $file));
echo substr($file_name,0,21);
echo "</div>";
}
}
}
}
else { // starts the split from keyword or no keyword
//***********************************************************************//
// sort folder names alphabetically, ignore case
natcasesort($files);
//Show the folders
foreach ($files as $file){
if ($file!="."&&$file!="..")
{
sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names
$extention = explode('.', $file);
if ($extention[1] == "")
{
echo "<div class='imgwrapper'>";
echo "<a href='?gallery=$gallery/$file'>";
echo "<img src='images/folder.jpg' border='0'>";
echo "<div class='folder'>";
echo current(explode('.', $file));
echo "</a>";
echo "</div>";
echo "</div>";
}
}
}
?>
<div style="clear:both"></div>
<?
//Show images
foreach ($files as $file){
if ($file!="."&&$file!="..")
{
$extention = explode('.', $file);
if ($extention[1] != "")
{
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
echo"</a><br/>";
echo "<div class='title'>";
$file_name = current(explode('.', $file));
echo substr($file_name,0,125);
echo "</div>";
echo "</div>";
}
}
}
}
?>
I have the area commented out where I believe the search string would be executed, as the display code is already there. I've tried a few things that didn't work, so didn't bother listing any of it.
Any ideas if I'm going about this the wrong way?
Thanks in advance.
if ($extention[1] != "")
{
if(stripos($file, $keyword) !== false)
{...
}
}
See (stripos() manual)
I was able to get this resolved from some outside help.
if ($file!="."&&$file!="..")
{
sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names
$extention = explode('.', $file);
if ($extention[1] == "")
{
$dir = "pics/";
$dir = $dir.$file."/";
$dirhandler = opendir($dir);
// read all the files from directory
$nofiles=0;
$files2=array();
while ($file2 = readdir($dirhandler)) {
if ($file2 != '.' && $file2 != '..')
{
$nofiles++;
$files2[$nofiles]=$file2;
}
}
closedir($dirhandler);
sort($files2); //Sorts the array (file names) alphabetically -- not the directory/folder names
//Show images
foreach ($files2 as $file2){
if ($file2!="."&&$file2!="..")
{
$extention = explode('.', $file2);
if ($extention[1] != "" && stripos($file2,$keyword)!==false)
{
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group'' href='$dir$file2' return false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file2&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
echo"</a><br/>";
echo "<div class='title'>";
$file2_name = current(explode('.', $file2));
echo substr($file2_name,0,125);
echo "</div>";
echo "</div>";
}
}
}
}