Log in

View Full Version : Resolved Getting file directories



Deadweight
10-01-2014, 09:46 PM
So I am using AJAX with PHP to get the folders inside the current directory I am in. Well at least, im attempting to.
JQuery:


$(function(){
getFolders('#folders');
})
function getFolders(id){
var path = window.location.pathname;

$.ajax({
type: "POST",
url: "get.php", //Your required php page
data: {type: "folders", dir: path}, //pass your required data here
success: function(response){
$(id).append(response);
}
});
}


PHP page (get.php):


$hiddenFolders = array('copyfiles','css','images','js');
$dir = trim($_POST['dir']);
$type = $_POST['type'];

if($type == "folders")
$new_array = array_diff(ffnames($dir, $type), $hiddenFolders);

echo '<tr><td>Check: '.count($new_array).'</td></tr>';


function ffnames($dir, $type){
$everything = array();
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..'))){
if($type == "folders" && is_dir($dir.$file)){
array_push($everything,$file);
}else if($type == "files" && !is_dir($dir.$file)){
array_push($everything,$file);
}
}
}
}

return $everything;
}


However it says that 'dir' doesnt exist (doing this from a localhost but will also be on server)

Deadweight
10-01-2014, 10:43 PM
Actually i figured it out:


$hiddenFolders = array('copyfiles','css','images','js');
$dir = './';
$type = $_POST['type'];

if($type == "folders"){
$new_array = array();
$new_array = array_diff(ffnames($dir, $type), $hiddenFolders);
}
//count(scandir($dir));
//$new_array = array();
//$new_array = ffnames($dir, $type);
$table = '<tr>';
foreach($new_array as $value){
$table .= '<td>'.$value.'</td>';
}
$table .= '</tr>';
echo $table;

function ffnames($dir, $type){
$everything = array();
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..'))){//All Objects
if($type == "folders" && is_dir($dir.$file)){
array_push($everything,$file);
}else if($type == "files" && !is_dir($dir.$file)){
array_push($everything,$file);
}
}
}
}

return $everything;
}