Pobranie zawartości katalogu z podziałem na pliki i katalogi - AJAX i PHP
Skrypt pobiera zawartość katalogu i wyświetla go z podziałem na pliki i katalogi w nim zawarte.
Listing
//index.html #############################################
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ajax</title>
<link href="ajaxcss.css" rel="stylesheet" type="text/css">
<script src="ajaxcore.js" type="text/javascript"></script>
<script src="ajax.js" type="text/javascript"></script>
</head>
<body>
<div id="mainDiv" class="mainDiv">
Katalog
<input type="text" value=""
id="tfDir" class="myTextInput">
<input type="button" value="Pobierz" class="myButton"
onclick="startRequest();" id="btnPobierz">
</div>
<div id="dataDiv" class="dataDiv">
</div>
</body>
</html>
//dane.php #############################################
<?php
$dirs = Array(
"/var/downloads/",
"/tmp/files/",
"./"
);
if(isSet($_GET["dir"]) && $_GET["dir"] != ""){
$dir = $_GET["dir"];
if(in_array($dir, $dirs)){
if(!$fd = @opendir($dir)){
echo "Zawartość katalogu nie jest niedostępna!";
exit;
}
$directories = Array();
$files = Array();
$others = Array();
$contents = "";
while (($file = readdir($fd)) !== false){
if($file != "." && $file != ".."){
if(is_dir($dir."/".$file)){
$directories[] = $file;
}
else if(is_file($dir."/".$file)){
$files[] = $file;
}
else{
$others[] = $file;
}
}
}
closedir($fd);
?>
<table style="width:100%;">
<tr><td style="background-color:#E0E0E0;text-align:center">
Katalogi:</td></tr>
<?php foreach($directories as $val) echo "<tr><td>$val</td></tr>"; ?>
<tr><td style="background-color:#E0E0E0;text-align:center">
Pliki:</td></tr>
<?php foreach($files as $val) echo "<tr><td>$val</td></tr>"; ?>
<tr><td style="background-color:#E0E0E0;text-align:center">
Inne:</td></tr>
<?php
foreach($others as $val) echo "<tr><td>$val</td></tr>";
}
else{
echo "Nieprawidłowy katalog!";
}
?>
</table>
<?php
}
else{
echo "Niepoprawne dane.";
}
?>
//ajaxcss.css #############################################
.mainDiv
{
color: #333333;
position: relative;
background-color: #EFEFEF;
border: 1px solid #000000;
margin: 10px 10px 10px 10px;
padding: 14px 14px 14px 14px;
width: 350px;
visibility: visible;
}
.dataDiv
{
color: #333333;
position: relative;
background-color: #ffffff;
border: 1px solid #000000;
margin: 10px 10px 10px 10px;
padding: 14px 14px 14px 14px;
width: 420px;
visibility: visible;
}
.myButton
{
font-family:helvetica,sans-serif;
font-size:84%;
font-weight:bold;
border:1px solid;
border-top-color:#696;
border-left-color:#696;
border-right-color:#363;
border-bottom-color:#363;
width:120px;
margin:5px;
}
.myTextInput
{
border:1px solid;
background-color: #F8F8F8;
border-top-color:#696;
border-left-color:#696;
border-right-color:#363;
border-bottom-color:#363;
width:70%;
}
//ajaxcore.js #############################################
function getXMLHttpRequestObject()
{
try{
return new XMLHttpRequest();
}
catch(e){
try{
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
return false;
}
}
}
function startGETRequest(url, onComplete, onEnd)
{
var XMLHttpRequestObject = getXMLHttpRequestObject();
if(XMLHttpRequestObject){
XMLHttpRequestObject.open("GET", url);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4){
if(XMLHttpRequestObject.status == 200){
var responseXML = XMLHttpRequestObject.responseXML;
var responseText = XMLHttpRequestObject.responseText;
onComplete(responseText, responseXML);
}
delete XMLHttpRequestObject;
onEnd();
}
}
XMLHttpRequestObject.send(null);
}
}
function startPOSTRequest(url, params, onComplete, onEnd)
{
var XMLHttpRequestObject = getXMLHttpRequestObject();
if(XMLHttpRequestObject){
XMLHttpRequestObject.open("POST", url);
XMLHttpRequestObject.setRequestHeader(
'Content-Type', 'application/x-www-form-urlencoded');
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4){
if(XMLHttpRequestObject.status == 200){
var responseXML = XMLHttpRequestObject.responseXML;
var responseText = XMLHttpRequestObject.responseText;
onComplete(responseText, responseXML);
}
delete XMLHttpRequestObject;
onEnd();
}
}
XMLHttpRequestObject.send(params);
}
}
//ajax.js #############################################
function startRequest()
{
tfDirText = document.getElementById('tfDir').value;
var btnPobierz = document.getElementById('btnPobierz');
btnPobierz.disabled = true;
url = "dane.php?dir=" + tfDirText;
url = encodeURI(url);
startGETRequest(url, onComplete, onEnd);
}
function onComplete(text, xml)
{
var div = document.getElementById('dataDiv');
div.innerHTML = text;
}
function onEnd()
{
var btnPobierz = document.getElementById('btnPobierz');
btnPobierz.disabled = false;
}
Dodano przez: igor
Ranga: Administrator serwisu Punktów: 0
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno