PHP - Script

 
Vista:

Script

Publicado por Marta (3 intervenciones) el 23/12/2007 13:23:04
Hola, lo primero felices fiestas a todos

A ver si me podéis echar una mano tengo un script que sirve para explorar los archivos y mostrar los ficheros (tamaño, fecha creación, fecha modificación, etc...), pero sólo quiero que me muestre de un directorio X y sus subdirectorios, con sus respectivos archivos, el script hace lo que quiero pero si le das a subir de nivel una vez estás en el directorio X, sigue subiendo y muestra todo mi árbol de directorios..... A ver si me explico mejor:

Otros Directorios
Directorio X
SubdirectorioX.1
Fichero1
Fichero2
SubdirectorioX.2
Fichero1
Fichero2

SubdirectorioX.N
Fichero1
Fichero2

Imaginad que tengo esta estructura y quiero mostrar los ficheros y subdirectorios de X con valores de creación, modificación, tamaño, etc.....

Pero no quiero que se pueda ver lo que hay encima de Directorio X pero si que se muevan entre los SubdirectoriosX.1... SubdirectoriosX.N; no sé si me he explicado bien.

Cómo podría solucionar esto?? Se os ocurre algo? Voy a poner el código del script a ver si me podéis ayudar, gracias a todos.

Un saludo

El código del script lo pongo en el siguiente mensaje... Gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Script

Publicado por Marta (3 intervenciones) el 23/12/2007 13:24:25
El Código del script:

Código de script:

<?php
// ---- version number ----
define( 'VERSION_INFO', 'v1.2' );

//======================================================================
// Parse all html tags
//======================================================================
function parse_tags($str) {
$trans = array( "&" => "&", '"' => """, "<" => "<", ">" => ">" );

return strtr(stripslashes($str), $trans);
}

//======================================================================
// download function
//======================================================================
function download($file){

$ext = substr(strrchr(basename($file), "."), 1);//get the file ext

header("Content-type: application/$ext");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename="".basename($file).""");
readfile($file);
}

//the file download if user click on it.
$fname = trim($_GET['fname']);
//this will make sure the file exist
if( !empty($fname) && file_exists($fname) && @fopen($fname, "rb") ) {
download($fname);
}

//Execute only if user input some code, executing move to below
$script = trim($_POST['editor']);
if( !empty($script) ) {

//value to put it back to the editor
$display = "inherit";
$value = "∧";
$editor = parse_tags($script);
}
else {
//close the editor
$display = "none";
$value = "∨";
$editor = "";
}

$self = basename(__FILE__);

$get_dir = $_GET['dir'];
$home = str_replace( "\", "/", dirname(__FILE__) );

//which folder to browse (failsafe)
if( !empty($_GET['dir']) && @opendir($get_dir) ) {
$dir = str_replace( "\", "/", $get_dir );
}
else { //default dir
$dir = $home;
}

//the search criteria to obey
$fname = trim($_POST['fname']);
$content = trim($_POST['content']);

//its searching now, set the search box open
if( !empty($fname) || !empty($content) ) {
$display2 = "inherit";
$value2 = "∧";

//value use to inherit back to the input
$fname_value = parse_tags($fname);
$content_value = parse_tags($content);

if($_POST['dir'] == 1) {
$subdirs = 1;
$check = " checked="checked"";
}
else {
$subdirs = 0;
$check = "";
}
}
else {
$display2 = "none";
$value2 = "∨";

//defaulr is checked
$check = " checked="checked"";
}

//======================================================================
// The main function of the program
//======================================================================

function loop_dir($dir) {
global $self, $fname, $content, $subdirs, $refresh, $dir_up, $all_dir, $unread_dir, $all_file, $unread_file;

//check if directory can be open
if ($handle = @opendir($dir)) {

//loop through the dir for FILES and DIR
while ( false != ($file = readdir($handle)) ) {

//increase the maximum execute time
set_time_limit(10);
$full_path = str_replace( "//", "/", $dir . "/" . $file);

if( $file == "." ) {
$refresh = "<tr bgcolor="#F5F5F5">
<td align="left"><img src="./sysimg/refresh.png" border="0" alt="" />
<a href="$self?dir=$dir">Refresh</a></td>
<td align="center"></td>
<td align="left"></td>
<td align="left"></td></tr> ";
}
elseif( $file == ".." ) {

$up_lvl = str_replace( "\", "/", dirname($dir . "..") );

$dir_up = "<tr bgcolor="#FFFFFF">


<td align="left"><a href="$self?dir=$up_lvl"><img src="./sysimg/back.png" border="0" alt="" /></a></td>
<td align="center"></td>
<td align="left"></td>
<td align="left"></td></tr> ";
}
//this is a directory, set the attr etc.
elseif( is_dir($full_path) ) {

$perm = substr(sprintf('%o', @fileperms("$full_path")), -4);
$time_mod = date("Y M d h:i A" ,filemtime($full_path));

//check if the dir can be open or not
if( @opendir($full_path) ) {

//loop to subdirs if specify by user
if( $subdirs == 1 ) {
loop_dir($full_path);
}

if( !empty($fname) ) { //searching of folder

if( stristr($file, $fname) ) { //search for the dir

//store all dirs in array
$all_dir[] .= "<td align="left"><img src="./ext_ico/folder.png" border="0" alt="" />
<a href="$self?dir=$full_path" title="$full_path">" . $file . "</a></td>
<td align="center">-</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}
elseif( stristr($file, $fname) ) { //search for the dir
//store all dirs in array
$all_dir[] .= "<td align="left"><img src="./ext_ico/folder.png" border="0" alt="" />
<a href="$self?dir=$full_path" title="$full_path">" . $file . "</a></td>
<td align="center">-</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}
}
elseif( empty($content) ) { //not searching, display them all

//store all dirs in array
$all_dir[] .= "<td align="left"><img src="./ext_ico/folder.png" border="0" alt="" />
<a href="$self?dir=$full_path" title="$full_path">" . $file . "</a></td>
<td align="center">-</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}

}
else { //the dir cannot be read
$unread_dir[] .= "<td align="left"><img src="./ext_ico/folder2.png" border="0" alt="" /> "
. $file . "</td>
<td align="center">-</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}

}
else {
//for normal file, these are the attr
$size = filesize($full_path);

if( $size >= 0 && $size < 1024 ) {
$size = $size . " B";
}
elseif( $size >= 1024 && $size < 1048576 ) { //round to KB
$size = round(($size/1024),2) . " KB";
}
elseif( $size >= 1048576 && $size < 1073741824 ) { //round to MB
$size = round(($size/1048576),2) . " MB";
}
elseif( $size >= 1073741824 ) { //round to GB
$size = round(($size/1073741824),2) . " GB";
}
else { //invalid size, error
$size = "--";
}

$perm = substr(sprintf('%o', @fileperms("$full_path")), -4);
$time_mod = date("Y M d h:i A" ,filemtime($full_path));

//check for icon for this filetype
$ext = substr(strrchr($file, "."), 1);

//icon for normal readable file
if( file_exists( "./ext_ico/" . $ext . ".png") ) {
$icon_normal = "./ext_ico/" . $ext . ".png";
}
else { //set as unknown filetype icon
$icon_normal = "./ext_ico/file.png";
}

//icon for unreadable file
if( file_exists( "./ext_ico/" . $ext . "2.png") ) {
$icon_unview = "./ext_ico/" . $ext . "2.png";
}
else { //set as unknown filetype icon
$icon_unview = "./ext_ico/file2.png";
}

//check if the file can be read
if( @fopen($full_path, "rb") ) {

//search for the content as well if user request
if( !empty($content) ) {
$file_data = file_get_contents($full_path);
}

//searching for files and content if so
if( !empty($fname) ) {

//search for the name
if( stristr($file, $fname) ) {

//store all files in array
$all_file[] .= " <!--$file!-->
<td align="left"><img src="$icon_normal" border="0" alt="" />
<a href="$self?fname=$full_path" title="$full_path">" . $file . "</a></td>
<td align="right">$size</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}
}
//search the file with part of this content
elseif( !empty($content) ) {

if( stristr($file_data, $content) ) {
//store all files in array
$all_file[] .= " <!--$file!-->
<td align="left"><img src="$icon_normal" border="0" alt="" />
<a href="$self?fname=$full_path" title="$full_path">" . $file . "</a></td>
<td align="right">$size</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}

}
elseif( empty($fname) ) {

//store all files in array
$all_file[] .= " <!--$file!-->
<td align="left"><img src="$icon_normal" border="0" alt="" />
<a href="$self?fname=$full_path" title="$full_path">" . $file . "</a></td>
<td align="right">$size</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}
}
else {
//file cannot be read
$unread_file[] .= " <!--$file!-->
<td align="left"><img src="$icon_unview" border="0" alt="" /> "
. $file . "</td>
<td align="right">$size</td>
<td align="center">$perm</td>
<td align="left">$time_mod</td>";
}
}
//incre the file number
$file_count++;
}

//display the files and dirs
@natcasesort($all_dir);
@natcasesort($unread_dir);
@natcasesort($all_file);
@natcasesort($unread_file);
}
}
//==============================End of main function==========================
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Script

Publicado por Marta (3 intervenciones) el 23/12/2007 13:25:16
CONTINUA EL SCRIPT:

//run the main function
loop_dir($dir);

//merge all the files and dirs which in in array
$all_files = @array_merge($all_dir, $unread_dir, $all_file, $unread_file);
$count = @count($all_files);

//the rows for up one level and refresh
$body = $dir_up . $refresh;

//if the dir isn't empty then loop them out
if( $count > 0 ) {

//the dirs part
$bg = "#F5F5F5";
for( $i = 0; $i < $count; $i++ ) {
//show all the files and folder with bgcolor switch
if( $bg == "#FFFFFF" ) {
$bg = "#F5F5F5";
}
else {
$bg = "#FFFFFF";
}
$body .= "<tr bgcolor="$bg">" . $all_files[$i] . "</tr> ";
}
}
//show the dir is empty
else {
$body .= "<tr bgcolor="#FFFFFF">
<td colspan="4" align="center" valign="middle" height="32">- No Files Found -</td>
</tr> ";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documentos</title>
<style type="text/css">
<!--
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
form {
display : inline;
margin : 0;
padding : 0;
}
img {
vertical-align : bottom;
}
h2 {
font-size : 20px;
}
a:visited,
a:link {
color : #002F62;
text-decoration : none;
}
a:hover {
color : #999999;
text-decoration : none;
}
.editor {
width : 596px;
background-color : #EAF8FF;
border : 1px solid #999999;
}
.but1 {
font-size : 10px;
margin : 0;
padding : 0 10px 0 10px;
}
.input {
font-size : 10px;
}
.info {
font-size : 10px;
color : #999999;
}
-->
</style>
</head>

<body>
<script type="text/javascript">
function open_close(id, oo, cc) {
ff = document.getElementById(id);

if( ff.style.display != "none" ) {
ff.style.display = "none";
return oo;
}
else {
ff.style.display = "";
return cc;
}
}
</script>
<h2> </h2>
<table align="center" border="0" style="border:1px solid #999999; background-color:#F5F5F5;" cellpadding="1" cellspacing="3">
<tr><td align="left" valign="top"><br />
<br />



<br />
<br />

<!--The search area!--> <br />
<br />

<table width="600" border="0" align="center" style="border:1px solid #999999;">
<tr bgcolor="#E1EEF4" height="22">
<td align="left">Name</td>
<td align="center" width="80">Size</td>
<td align="center" width="80">Permission</td>
<td align="center" width="150">Date Modified</td>
</tr>
<?php echo $body; ?>
</table>
</td></tr>
</table>
<br /><br />
<hr style="width:400px;" />
<center><font class="info">

</font></center>
</body>
</html>
<?php
if( !empty($script) ) {
//executing script
eval(stripslashes($script));
}
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar